|
|
|
@@ -32,18 +32,21 @@ func (s stubFileInfo) ModTime() time.Time { return s.mtime }
|
|
|
|
|
func (s stubFileInfo) IsDir() bool { return false }
|
|
|
|
|
func (s stubFileInfo) Sys() any { return nil }
|
|
|
|
|
|
|
|
|
|
// setupFreshenDir writes file1.txt and sub/file2.txt, holding "content1"
|
|
|
|
|
// and "content2", into a fresh temp dir and runs gen on it. It returns
|
|
|
|
|
// the temp dir and the path of the manifest gen wrote there.
|
|
|
|
|
func setupFreshenDir(t *testing.T, fs afero.Fs) (string, string) {
|
|
|
|
|
// setupFreshenDir writes files, by path and content, into a fresh temp
|
|
|
|
|
// dir and runs gen on it. It returns the temp dir and the path of the
|
|
|
|
|
// manifest gen wrote there.
|
|
|
|
|
func setupFreshenDir(
|
|
|
|
|
t *testing.T, fs afero.Fs, files map[string]string,
|
|
|
|
|
) (string, string) {
|
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
|
|
root := t.TempDir()
|
|
|
|
|
manifestPath := filepath.Join(root, defaultManifestName)
|
|
|
|
|
|
|
|
|
|
require.NoError(t, fs.MkdirAll(filepath.Join(root, "sub"), 0o750))
|
|
|
|
|
writeTestFile(t, fs, filepath.Join(root, "file1.txt"), "content1")
|
|
|
|
|
writeTestFile(t, fs, filepath.Join(root, "sub", "file2.txt"), "content2")
|
|
|
|
|
for path, content := range files {
|
|
|
|
|
require.NoError(t, fs.MkdirAll(filepath.Dir(filepath.Join(root, path)), 0o750))
|
|
|
|
|
writeTestFile(t, fs, filepath.Join(root, path), content)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
opts := testOpts([]string{testApp, cmdGenerate, "-q", "-o", manifestPath, root}, fs)
|
|
|
|
|
require.Equal(t, 0, runCLI(opts), "stderr: %s", testStderr(t, opts))
|
|
|
|
@@ -81,7 +84,8 @@ func TestFreshenUnchanged(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
|
root, manifestPath := setupFreshenDir(t, fs)
|
|
|
|
|
root, manifestPath := setupFreshenDir(t, fs,
|
|
|
|
|
map[string]string{testFileTxt: "content1", testDirFile: "content2"})
|
|
|
|
|
before := manifestFiles(t, fs, manifestPath)
|
|
|
|
|
|
|
|
|
|
runFreshen(t, fs, root, manifestPath)
|
|
|
|
@@ -140,52 +144,48 @@ func assertManifestLists(
|
|
|
|
|
func TestFreshenWithChanges(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
tree := map[string]string{testFileTxt: "content1", testDirFile: "content2"}
|
|
|
|
|
|
|
|
|
|
for _, tc := range []struct {
|
|
|
|
|
name string
|
|
|
|
|
write map[string]string // files to write, by path and content
|
|
|
|
|
remove string // file to delete, if any
|
|
|
|
|
want map[string]string // files the manifest must then list
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "modified file",
|
|
|
|
|
write: map[string]string{"sub/file2.txt": "modified content2"},
|
|
|
|
|
want: map[string]string{
|
|
|
|
|
"file1.txt": "content1",
|
|
|
|
|
"sub/file2.txt": "modified content2",
|
|
|
|
|
},
|
|
|
|
|
write: map[string]string{testDirFile: "modified content2"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "new file",
|
|
|
|
|
write: map[string]string{"sub/file3.txt": "content3"},
|
|
|
|
|
want: map[string]string{
|
|
|
|
|
"file1.txt": "content1",
|
|
|
|
|
"sub/file2.txt": "content2",
|
|
|
|
|
"sub/file3.txt": "content3",
|
|
|
|
|
},
|
|
|
|
|
write: map[string]string{"dir/new.txt": "content3"},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "deleted file",
|
|
|
|
|
remove: "file1.txt",
|
|
|
|
|
want: map[string]string{"sub/file2.txt": "content2"},
|
|
|
|
|
remove: testFileTxt,
|
|
|
|
|
},
|
|
|
|
|
} {
|
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
|
|
fs := afero.NewOsFs()
|
|
|
|
|
root, manifestPath := setupFreshenDir(t, fs)
|
|
|
|
|
root, manifestPath := setupFreshenDir(t, fs, tree)
|
|
|
|
|
|
|
|
|
|
// want is the tree as it is after the change.
|
|
|
|
|
want := maps.Clone(tree)
|
|
|
|
|
|
|
|
|
|
for path, content := range tc.write {
|
|
|
|
|
writeTestFile(t, fs, filepath.Join(root, path), content)
|
|
|
|
|
want[path] = content
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if tc.remove != "" {
|
|
|
|
|
require.NoError(t, fs.Remove(filepath.Join(root, tc.remove)))
|
|
|
|
|
delete(want, tc.remove)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
runFreshen(t, fs, root, manifestPath)
|
|
|
|
|
|
|
|
|
|
assertManifestLists(t, fs, root, manifestPath, tc.want)
|
|
|
|
|
assertManifestLists(t, fs, root, manifestPath, want)
|
|
|
|
|
|
|
|
|
|
opts := testOpts([]string{
|
|
|
|
|
testApp, cmdCheck, "-q", testFlagNoExtra, testFlagBase, root, manifestPath,
|
|
|
|
|