-
Notifications
You must be signed in to change notification settings - Fork 294
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
internal/vcs: refactor to prepare for LICENSE file inclusion
Including LICENSE files from the root of a VCS repo in the case where no LICENSE file is present in a subdirectory module requires that we perform all operations from the module root. We prepare for this by making ListFiles and Status take paths instead of implicitly operating on the module subdirectory. This allows us to perform separate checks on the root LICENSE file if one exists. Indeed the concept of a VCS implementation keeping track of a subdir is eliminated entirely. As part of this we expand the VCS test suite to include a number of scenarios that cover the "edges" of ListFiles and Status. We also fix a bug in passing where git ListFiles implementation did not correctly handle an empty result, i.e. the situation where no files match args. For #3130. Signed-off-by: Paul Jolly <[email protected]> Change-Id: Id669490a447a4f07ced611f3323868b4519b1919 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1195548 Unity-Result: CUE porcuepine <[email protected]> TryBot-Result: CUEcueckoo <[email protected]> Reviewed-by: Daniel Martí <[email protected]>
- Loading branch information
Showing
4 changed files
with
150 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,58 +44,148 @@ func TestGit(t *testing.T) { | |
err := copyFS(dir, testFS) | ||
qt.Assert(t, qt.IsNil(err)) | ||
|
||
_, err = New("git", filepath.Join(dir, "subdir")) | ||
// In the tests that follow, we are testing the scenario where a module is | ||
// present in $dir/subdir (the VCS is rooted at $dir). cue/load or similar | ||
// would establish the absolute path $dir/subdir is the CUE module root, and | ||
// as such we use that absolute path as an argument in the calls to the VCS | ||
// implementation. | ||
subdir := filepath.Join(dir, "subdir") | ||
|
||
_, err = New("git", subdir) | ||
qt.Assert(t, qt.ErrorMatches(err, `git VCS not found in any parent of ".+"`)) | ||
|
||
initTestEnv(t) | ||
mustRunCmd(t, dir, "git", "init") | ||
v, err := New("git", filepath.Join(dir, "subdir")) | ||
v, err := New("git", subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
|
||
// The status shows that we have uncommitted files | ||
// because we haven't yet added the files after doing | ||
// git init. | ||
status, err := v.Status(ctx) | ||
statusuncommitted, err := v.Status(ctx, subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.IsTrue(status.Uncommitted)) | ||
qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) | ||
|
||
mustRunCmd(t, dir, "git", "add", ".") | ||
status, err = v.Status(ctx) | ||
statusuncommitted, err = v.Status(ctx, subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.IsTrue(status.Uncommitted)) | ||
qt.Assert(t, qt.IsTrue(statusuncommitted.Uncommitted)) | ||
|
||
commitTime := time.Now().Truncate(time.Second) | ||
mustRunCmd(t, dir, "git", | ||
"-c", "[email protected]", | ||
"-c", "user.name=cueckoo", | ||
"commit", "-m", "something", | ||
) | ||
status, err = v.Status(ctx) | ||
status, err := v.Status(ctx, subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.IsFalse(status.Uncommitted)) | ||
qt.Assert(t, qt.IsTrue(!status.CommitTime.Before(commitTime))) | ||
qt.Assert(t, qt.Matches(status.Revision, `[0-9a-f]+`)) | ||
files, err := v.ListFiles(ctx, filepath.Join(dir, "subdir")) | ||
|
||
// Test various permutations of ListFiles | ||
var files []string | ||
allFiles := []string{ | ||
"bar.txt", | ||
"baz/something", | ||
"subdir/bar/baz", | ||
"subdir/foo", | ||
} | ||
|
||
// Empty dir implies repo root, i.e. all files | ||
files, err = v.ListFiles(ctx, "") | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
|
||
// Explicit repo root | ||
files, err = v.ListFiles(ctx, dir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
|
||
// Relative path file under repo root | ||
files, err = v.ListFiles(ctx, dir, "bar.txt") | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, []string{"bar.txt"})) | ||
|
||
// Absolute path file under repo root | ||
files, err = v.ListFiles(ctx, dir, filepath.Join(dir, "bar.txt")) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, []string{"bar.txt"})) | ||
|
||
// Relative path sub directory listed from root | ||
files, err = v.ListFiles(ctx, dir, "subdir") | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, []string{ | ||
"bar/baz", | ||
"foo", | ||
"subdir/bar/baz", | ||
"subdir/foo", | ||
})) | ||
files, err = v.ListFiles(ctx, dir) | ||
|
||
// Absolute path sub directory listed from root | ||
files, err = v.ListFiles(ctx, dir, filepath.Join(dir, "subdir")) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, []string{ | ||
"bar.txt", | ||
"baz/something", | ||
"subdir/bar/baz", | ||
"subdir/foo", | ||
})) | ||
|
||
// Change a file that's not in subdir. The status should remain the same. | ||
// Listing of files in sub directory | ||
files, err = v.ListFiles(ctx, subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, []string{ | ||
"bar/baz", | ||
"foo", | ||
})) | ||
|
||
// Change a file that's not in subdir. The status in subdir should remain | ||
// the same. | ||
err = os.WriteFile(filepath.Join(dir, "bar.txt"), []byte("something else"), 0o666) | ||
qt.Assert(t, qt.IsNil(err)) | ||
status1, err := v.Status(ctx) | ||
statuschanged, err := v.Status(ctx) | ||
qt.Assert(t, qt.IsTrue(statuschanged.Uncommitted)) | ||
status1, err := v.Status(ctx, subdir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(status1, status)) | ||
|
||
// Restore the file and ensure Status is clean | ||
err = os.WriteFile(filepath.Join(dir, "bar.txt"), nil, 0o666) | ||
qt.Assert(t, qt.IsNil(err)) | ||
files, err = v.ListFiles(ctx, dir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
status2, err := v.Status(ctx) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(status2, status)) | ||
|
||
// Add an untracked file | ||
untracked := filepath.Join(dir, "untracked") | ||
err = os.WriteFile(untracked, nil, 0666) | ||
qt.Assert(t, qt.IsNil(err)) | ||
files, err = v.ListFiles(ctx, dir) // Does not include untracked file | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
statusuntracked, err := v.Status(ctx) // Status does now show uncommitted changes | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.IsTrue(statusuntracked.Uncommitted)) | ||
|
||
// Remove the untracked file and ensure Status is clean | ||
err = os.Remove(untracked) | ||
qt.Assert(t, qt.IsNil(err)) | ||
files, err = v.ListFiles(ctx, dir) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
status3, err := v.Status(ctx) | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(status3, status)) | ||
|
||
// // Remove a tracked file so that it is now "missing" | ||
err = os.Remove(filepath.Join(dir, "bar.txt")) | ||
qt.Assert(t, qt.IsNil(err)) | ||
files, err = v.ListFiles(ctx, dir) // Still reports "missing" file | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.DeepEquals(files, allFiles)) | ||
statusmissing, err := v.Status(ctx) // Status does now show uncommitted changes | ||
qt.Assert(t, qt.IsNil(err)) | ||
qt.Assert(t, qt.IsTrue(statusmissing.Uncommitted)) | ||
} | ||
|
||
// initTestEnv sets up the environment so that | ||
|