//nolint:testpackage // white-box test of unexported internals package cli import ( "bytes" "testing" "github.com/spf13/afero" "github.com/spf13/cobra" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" ) // TestAddDropsOneTrailingNewlineImportKeepsIt pins the rule the README // gives: `secret add` drops one trailing newline from stdin, and // `secret import` stores the file's bytes unchanged. func TestAddDropsOneTrailingNewlineImportKeepsIt(t *testing.T) { t.Parallel() fs, vlt := newSizeTestVault(t) value := []byte("a\n\n") cmd := &cobra.Command{} cmd.SetIn(bytes.NewReader(value)) cli := NewCLIInstanceWithStateDir(fs, testStateDir) cli.cmd = cmd require.NoError(t, cli.AddSecret("added", false)) sourceFile := "/test/value" require.NoError(t, afero.WriteFile(fs, sourceFile, value, 0o600)) require.NoError(t, cli.ImportSecret(cmd, "imported", sourceFile, false)) added, err := vlt.GetSecret("added") require.NoError(t, err) defer added.Destroy() imported, err := vlt.GetSecret("imported") require.NoError(t, err) defer imported.Destroy() assert.Equal(t, []byte("a\n"), added.Bytes()) assert.Equal(t, []byte("a\n\n"), imported.Bytes()) }