package cli //nolint:testpackage // shares runEntry and the hermetic config helpers import ( "fmt" "os" "path/filepath" "testing" "github.com/adrg/xdg" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestEntrySnapshotCreateSkipsOwnFiles backs up a directory that holds // vaultik's index and PID file beside one other file, with no exclude // pattern set. The summary counts only the other file. See // https://git.eeqj.de/sneak/vaultik/issues/421. // // Not parallel: it replaces os.Args, os.Stdout, os.Stderr and the xdg // globals. func TestEntrySnapshotCreateSkipsOwnFiles(t *testing.T) { for _, testCase := range []struct { name string // linked sets index_path to a symlink outside the backed-up // directory that points to the index inside it. linked bool }{ {name: "index in the backed-up directory"}, {name: "index_path links into the backed-up directory", linked: true}, } { t.Run(testCase.name, func(t *testing.T) { dir := t.TempDir() configPath := filepath.Join(dir, "config.yml") sourceDir := filepath.Join(dir, "source") storeDir := filepath.Join(dir, "store") indexPath := filepath.Join(sourceDir, "index.sqlite") if testCase.linked { link := filepath.Join(dir, "index.sqlite") require.NoError(t, os.Symlink(indexPath, link)) indexPath = link } contents := fmt.Sprintf(hermeticConfig, sourceDir, storeDir, indexPath) require.NoError(t, os.WriteFile(configPath, []byte(contents), configFileMode)) require.NoError(t, os.Mkdir(storeDir, 0o750)) require.NoError(t, os.Mkdir(sourceDir, 0o750)) require.NoError(t, os.WriteFile( filepath.Join(sourceDir, "file.txt"), []byte("hello"), 0o600)) // The PID file goes in a directory under XDG_DATA_HOME, which // xdg reads at package init. t.Setenv("XDG_DATA_HOME", filepath.Join(sourceDir, "data")) xdg.Reload() t.Cleanup(xdg.Reload) code, stdout, _ := runEntry(t, flagConfig, configPath, cmdSnapshot, cmdCreate) require.Equal(t, 0, code) assert.Contains(t, stdout, "Files: 1 examined, 1 backed up, 0 unchanged.") }) } }