package cli //nolint:testpackage // shares runEntry and the backup config helpers import ( "encoding/json" "os" "path/filepath" "testing" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "sneak.berlin/go/vaultik/internal/vaultik" ) // cmdFiles is the word of `snapshot files`, used to build argument // vectors below. const cmdFiles = "files" // TestEntrySnapshotFilesListsOnlyThePath backs up two files and runs // `snapshot files` with the snapshot ID and the path of one of them, // which must be the only file listed. // //nolint:paralleltest // replaces os.Args, os.Stdout, os.Stderr and the xdg globals func TestEntrySnapshotFilesListsOnlyThePath(t *testing.T) { configPath, _ := writeBackupConfigWithPrivateKey(t) // writeBackupConfig puts file.txt in the source directory beside the // config file. sourceDir := filepath.Join(filepath.Dir(configPath), "source") listed := filepath.Join(sourceDir, "file.txt") require.NoError(t, os.WriteFile( filepath.Join(sourceDir, "other.txt"), []byte("other"), 0o600)) code, _, stderr := runEntry(t, flagConfig, configPath, cmdSnapshot, cmdCreate) require.Equal(t, 0, code, stderr) code, stdout, stderr := runEntry(t, flagConfig, configPath, cmdSnapshot, cmdFiles, onlySnapshotID(t, configPath), listed, flagJSON) require.Equal(t, 0, code, stderr) var entries []vaultik.ListedFile require.NoError(t, json.Unmarshal([]byte(stdout), &entries)) require.Len(t, entries, 1, "only the path given may be listed") assert.Equal(t, listed, entries[0].Path) }