Skip to content

Commit

Permalink
internal/cuetxtar: simplify ordering
Browse files Browse the repository at this point in the history
Instead of reconstructing the order, now simply
mark the order of new files to be that of files
they are related too. This simplifies the code
and actually results in a more precise ordering.

Note that since we add new files first, derivative files will be added before their
related files. This mimics the behavior we
had so far.

Signed-off-by: Marcel van Lohuizen <[email protected]>
Change-Id: I97553dcc7ebfb984bdef22a1b1925fbd0e866b1d
Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1199804
TryBot-Result: CUEcueckoo <[email protected]>
Unity-Result: CUE porcuepine <[email protected]>
Reviewed-by: Matthew Sackman <[email protected]>
  • Loading branch information
mpvl committed Aug 21, 2024
1 parent eb3b539 commit 2a84338
Showing 1 changed file with 12 additions and 28 deletions.
40 changes: 12 additions & 28 deletions internal/cuetxtar/txtar.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"fmt"
"io"
"io/fs"
"maps"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -418,13 +419,6 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
t.Fatalf("error parsing txtar file: %v", err)
}

// Record ordering of files in the archive to preserve that ordering
// later.
ordering := map[string]int{}
for i, f := range a.Files {
ordering[f.Name] = i
}

tc := &Test{
T: t,
Archive: a,
Expand Down Expand Up @@ -487,13 +481,20 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
index[f.Name] = i
}

// Record ordering of files in the archive to preserve that ordering
// later.
ordering := maps.Clone(index)

// Add diff files between fallback and main file. These are added
// as regular output files so that they can be updated as well.
for _, sub := range tc.outFiles {
if sub.fallback == sub.name {
continue
}
if j, ok := index[sub.fallback]; ok {
if _, ok := ordering[sub.name]; !ok {
ordering[sub.name] = j
}
fallback := a.Files[j].Data

result := sub.buf.Bytes()
Expand All @@ -502,6 +503,9 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
}

diffName := "diff/-" + sub.name + "<==>+" + sub.fallback
if _, ok := ordering[diffName]; !ok {
ordering[diffName] = j
}
switch diff := diff.Diff("old", fallback, "new", result); {
case len(diff) > 0:
tc.outFiles = append(tc.outFiles, file{
Expand Down Expand Up @@ -540,27 +544,7 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {
}
}

// Insert results of this test at first location of any existing
// test or at end of list otherwise.
k := len(a.Files)
for _, sub := range tc.outFiles {
if i, ok := index[sub.name]; ok {
k = i
break
}
if i, ok := index[sub.fallback]; ok {
k = i
break
}
}

// Filter files up to k from the original list.
files := make([]txtar.File, 0, len(a.Files))
for _, f := range a.Files[:k] {
if _, ok := index[f.Name]; ok {
files = append(files, f)
}
}

for _, sub := range tc.outFiles {
result := sub.buf.Bytes()
Expand Down Expand Up @@ -607,7 +591,7 @@ func (x *TxTarTest) run(t *testing.T, f func(tc *Test)) {

// Add remaining unrelated files, ignoring files that were already
// added.
for _, f := range a.Files[k:] {
for _, f := range a.Files {
if _, ok := index[f.Name]; ok {
files = append(files, f)
}
Expand Down

0 comments on commit 2a84338

Please sign in to comment.