move test stuff to internal/testdata

This commit is contained in:
Roberto Hidalgo 2023-11-26 10:31:15 -06:00
parent d9863596a2
commit 47a8f1e2a4
13 changed files with 131 additions and 127 deletions

View File

@ -8,16 +8,17 @@ import (
"testing" "testing"
. "git.rob.mx/nidito/joao/cmd" . "git.rob.mx/nidito/joao/cmd"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata"
fixtures "git.rob.mx/nidito/joao/internal/testing" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
"github.com/1Password/connect-sdk-go/onepassword" "github.com/1Password/connect-sdk-go/onepassword"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func TestFetch(t *testing.T) { func TestFetch(t *testing.T) {
fixtures.EnableDebugLogging() testdata.EnableDebugLogging()
mockOPConnect(t) testdata.MockOPConnect(t)
cfg := fixtures.NewTestConfig("some:test") root := testdata.FromProjectRoot()
cfg := testdata.NewTestConfig("some:test")
cfg.Sections = append(cfg.Sections, cfg.Sections = append(cfg.Sections,
&onepassword.ItemSection{ID: "o", Label: "o"}, &onepassword.ItemSection{ID: "o", Label: "o"},
&onepassword.ItemSection{ID: "e-fez-tambem", Label: "e-fez-tambem"}, &onepassword.ItemSection{ID: "e-fez-tambem", Label: "e-fez-tambem"},
@ -52,8 +53,7 @@ func TestFetch(t *testing.T) {
Value: "quém!", Value: "quém!",
}) })
mock.Add(cfg) opconnect.Add(cfg)
root := fromProjectRoot()
out := &bytes.Buffer{} out := &bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
cmd.Flags().Bool("dry-run", true, "") cmd.Flags().Bool("dry-run", true, "")
@ -62,13 +62,13 @@ func TestFetch(t *testing.T) {
Fetch.SetBindings() Fetch.SetBindings()
Fetch.Cobra = cmd Fetch.Cobra = cmd
err := Fetch.Run(cmd, []string{root + "/test.yaml"}) err := Fetch.Run(cmd, []string{testdata.YAML("test")})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
} }
expected := `--- /Users/roberto/src/joao/test.yaml expected := `--- ` + root + `/testdata/test.yaml
+++ op://example/some:test +++ op://example/some:test
@@ -1,4 +1,8 @@ @@ -1,4 +1,8 @@
bool: false bool: false

View File

@ -6,49 +6,24 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"os" "os"
"path"
"runtime"
"strings" "strings"
"testing" "testing"
. "git.rob.mx/nidito/joao/cmd" . "git.rob.mx/nidito/joao/cmd"
opclient "git.rob.mx/nidito/joao/internal/op-client" "git.rob.mx/nidito/joao/internal/testdata"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
fixtures "git.rob.mx/nidito/joao/internal/testing"
"github.com/1Password/connect-sdk-go/connect"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
func mockOPConnect(t *testing.T) {
t.Helper()
opclient.ConnectClientFactory = func(host, token, userAgent string) connect.Client {
return &mock.Client{}
}
client := opclient.NewConnect("", "")
opclient.Use(client)
mock.Clear()
}
func fromProjectRoot() string {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "../")
if err := os.Chdir(dir); err != nil {
panic(err)
}
wd, _ := os.Getwd()
return wd
}
func TestGetBadYAML(t *testing.T) { func TestGetBadYAML(t *testing.T) {
root := fromProjectRoot()
Get.SetBindings() Get.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
err := Get.Run(cmd, []string{root + "/bad-test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("bad-test"), "."})
if err == nil { if err == nil {
t.Fatalf("Did not throw on bad path: %s", out.String()) t.Fatalf("Did not throw on bad path: %s", out.String())
} }
@ -60,7 +35,7 @@ func TestGetBadYAML(t *testing.T) {
} }
func TestGetBadPath(t *testing.T) { func TestGetBadPath(t *testing.T) {
root := fromProjectRoot() root := testdata.FromProjectRoot()
Get.SetBindings() Get.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -80,7 +55,6 @@ func TestGetBadPath(t *testing.T) {
} }
func TestGetNormal(t *testing.T) { func TestGetNormal(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -88,13 +62,13 @@ func TestGetNormal(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
} }
expected, err := os.ReadFile(root + "/test.yaml") expected, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file: %s", err) t.Fatalf("could not read file: %s", err)
} }
@ -105,7 +79,6 @@ func TestGetNormal(t *testing.T) {
} }
func TestGetRedacted(t *testing.T) { func TestGetRedacted(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -113,14 +86,14 @@ func TestGetRedacted(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", ".", "--redacted"} os.Args = []string{testdata.YAML("test"), ".", "--redacted"}
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
} }
expected, err := os.ReadFile(root + "/test.yaml") expected, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file: %s", err) t.Fatalf("could not read file: %s", err)
} }
@ -131,7 +104,6 @@ func TestGetRedacted(t *testing.T) {
} }
func TestGetPath(t *testing.T) { func TestGetPath(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -140,7 +112,7 @@ func TestGetPath(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", "nested.secret"} os.Args = []string{testdata.YAML("test"), "nested.secret"}
err := Get.Run(cmd, os.Args) err := Get.Run(cmd, os.Args)
if err != nil { if err != nil {
@ -155,8 +127,8 @@ func TestGetPath(t *testing.T) {
out = bytes.Buffer{} out = bytes.Buffer{}
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
os.Args = []string{root + "/test.yaml", "nested", "--output", "diff-yaml"} os.Args = []string{testdata.YAML("test"), "nested", "--output", "diff-yaml"}
err = Get.Run(cmd, []string{root + "/test.yaml", "nested"}) err = Get.Run(cmd, []string{testdata.YAML("test"), "nested"})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -178,7 +150,6 @@ string: quem`
} }
func TestGetPathCollection(t *testing.T) { func TestGetPathCollection(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -187,8 +158,8 @@ func TestGetPathCollection(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", "nested", "--output", "yaml"} os.Args = []string{testdata.YAML("test"), "nested", "--output", "yaml"}
err := Get.Run(cmd, []string{root + "/test.yaml", "nested"}) err := Get.Run(cmd, []string{testdata.YAML("test"), "nested"})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -210,7 +181,6 @@ string: quem`
} }
func TestGetDiff(t *testing.T) { func TestGetDiff(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -219,8 +189,8 @@ func TestGetDiff(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", ".", "--output", "diff-yaml"} os.Args = []string{testdata.YAML("test"), ".", "--output", "diff-yaml"}
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -254,7 +224,6 @@ string: pato`
} }
func TestGetJSON(t *testing.T) { func TestGetJSON(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -263,8 +232,8 @@ func TestGetJSON(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", ".", "--output", "json"} os.Args = []string{testdata.YAML("test"), ".", "--output", "json"}
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -278,7 +247,6 @@ func TestGetJSON(t *testing.T) {
} }
func TestGetDeepJSON(t *testing.T) { func TestGetDeepJSON(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -287,8 +255,9 @@ func TestGetDeepJSON(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/deeply-nested.test.yaml", ".", "--output", "json"} file := testdata.YAML("deeply-nested.test")
err := Get.Run(cmd, []string{root + "/deeply-nested.test.yaml", "."}) os.Args = []string{file, ".", "--output", "json"}
err := Get.Run(cmd, []string{file, "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -302,7 +271,6 @@ func TestGetDeepJSON(t *testing.T) {
} }
func TestGetJSONPathScalar(t *testing.T) { func TestGetJSONPathScalar(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -311,8 +279,9 @@ func TestGetJSONPathScalar(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", "nested.secret", "--output", "json"} file := testdata.YAML("test")
err := Get.Run(cmd, []string{root + "/test.yaml", "nested.secret"}) os.Args = []string{file, "nested.secret", "--output", "json"}
err := Get.Run(cmd, []string{file, "nested.secret"})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -326,7 +295,6 @@ func TestGetJSONPathScalar(t *testing.T) {
} }
func TestGetJSONPathCollection(t *testing.T) { func TestGetJSONPathCollection(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -335,8 +303,9 @@ func TestGetJSONPathCollection(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", "nested", "--output", "json"} file := testdata.YAML("test")
err := Get.Run(cmd, []string{root + "/test.yaml", "nested"}) os.Args = []string{file, "nested", "--output", "json"}
err := Get.Run(cmd, []string{file, "nested"})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -350,7 +319,6 @@ func TestGetJSONPathCollection(t *testing.T) {
} }
func TestGetJSONRedacted(t *testing.T) { func TestGetJSONRedacted(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -359,8 +327,9 @@ func TestGetJSONRedacted(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", ".", "--output", "json", "--redacted"} file := testdata.YAML("test")
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) os.Args = []string{file, ".", "--output", "json", "--redacted"}
err := Get.Run(cmd, []string{file, "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
@ -374,7 +343,6 @@ func TestGetJSONRedacted(t *testing.T) {
} }
func TestGetJSONOP(t *testing.T) { func TestGetJSONOP(t *testing.T) {
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -382,14 +350,14 @@ func TestGetJSONOP(t *testing.T) {
cmd.SetOut(&out) cmd.SetOut(&out)
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
os.Args = []string{root + "/test.yaml", ".", "--output", "op"} os.Args = []string{testdata.YAML("test"), ".", "--output", "op"}
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)
} }
cfg := fixtures.NewTestConfig("some:test") cfg := testdata.NewTestConfig("some:test")
id := cfg.ID id := cfg.ID
cfg.ID = "" cfg.ID = ""
defer func() { cfg.ID = id }() defer func() { cfg.ID = id }()
@ -404,9 +372,8 @@ func TestGetJSONOP(t *testing.T) {
} }
func TestGetRemote(t *testing.T) { func TestGetRemote(t *testing.T) {
mockOPConnect(t) testdata.MockOPConnect(t)
mock.Add(fixtures.NewTestConfig("some:test")) opconnect.Add(testdata.NewTestConfig("some:test"))
root := fromProjectRoot()
out := bytes.Buffer{} out := bytes.Buffer{}
Get.SetBindings() Get.SetBindings()
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -417,8 +384,8 @@ func TestGetRemote(t *testing.T) {
cmd.SetErr(&out) cmd.SetErr(&out)
Get.Cobra = cmd Get.Cobra = cmd
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
os.Args = []string{root + "/test.yaml", "."} os.Args = []string{testdata.YAML("test"), "."}
err := Get.Run(cmd, []string{root + "/test.yaml", "."}) err := Get.Run(cmd, []string{testdata.YAML("test"), "."})
if err != nil { if err != nil {
t.Fatalf("could not get: %s", err) t.Fatalf("could not get: %s", err)

View File

@ -11,7 +11,7 @@ import (
"testing" "testing"
. "git.rob.mx/nidito/joao/cmd" . "git.rob.mx/nidito/joao/cmd"
"github.com/sirupsen/logrus" "git.rob.mx/nidito/joao/internal/testdata"
"github.com/spf13/cobra" "github.com/spf13/cobra"
) )
@ -24,7 +24,7 @@ func tempTestYaml(root, name string, data []byte) (string, func(), error) {
} }
func TestSet(t *testing.T) { func TestSet(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -38,9 +38,9 @@ func TestSet(t *testing.T) {
cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("delete", false, "")
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read test file")
} }
path, cleanup, err := tempTestYaml(root, "set-plain", original) path, cleanup, err := tempTestYaml(root, "set-plain", original)
@ -73,7 +73,7 @@ string: |-
} }
func TestSetSecret(t *testing.T) { func TestSetSecret(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set-secret")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -87,7 +87,7 @@ func TestSetSecret(t *testing.T) {
cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("delete", false, "")
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -119,7 +119,7 @@ func TestSetSecret(t *testing.T) {
} }
func TestSetFromFile(t *testing.T) { func TestSetFromFile(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set-from-file")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -130,7 +130,7 @@ func TestSetFromFile(t *testing.T) {
cmd.Flags().Bool("delete", false, "") cmd.Flags().Bool("delete", false, "")
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -168,7 +168,7 @@ func TestSetFromFile(t *testing.T) {
} }
func TestSetNew(t *testing.T) { func TestSetNew(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set-new")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -183,7 +183,7 @@ func TestSetNew(t *testing.T) {
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -219,7 +219,7 @@ quarteto: |-
} }
func TestSetNested(t *testing.T) { func TestSetNested(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set-nested")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -234,7 +234,7 @@ func TestSetNested(t *testing.T) {
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -267,7 +267,7 @@ func TestSetNested(t *testing.T) {
} }
func TestSetJSON(t *testing.T) { func TestSetJSON(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-set-json")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -282,7 +282,7 @@ func TestSetJSON(t *testing.T) {
cmd.Flags().Bool("json", true, "") cmd.Flags().Bool("json", true, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -317,9 +317,7 @@ na-beira-da-lagoa:
} }
func TestSetList(t *testing.T) { func TestSetList(t *testing.T) {
logrus.SetLevel(logrus.DebugLevel) root := testdata.TempDir(t, "test-set-list")
root := fromProjectRoot()
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -334,7 +332,7 @@ func TestSetList(t *testing.T) {
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -368,7 +366,7 @@ asdf:
} }
func TestDelete(t *testing.T) { func TestDelete(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-delete")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -380,7 +378,7 @@ func TestDelete(t *testing.T) {
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }
@ -414,7 +412,7 @@ string: pato
} }
func TestDeleteNested(t *testing.T) { func TestDeleteNested(t *testing.T) {
root := fromProjectRoot() root := testdata.TempDir(t, "test-delete-nested")
Set.SetBindings() Set.SetBindings()
out := bytes.Buffer{} out := bytes.Buffer{}
cmd := &cobra.Command{} cmd := &cobra.Command{}
@ -426,7 +424,7 @@ func TestDeleteNested(t *testing.T) {
cmd.Flags().Bool("json", false, "") cmd.Flags().Bool("json", false, "")
cmd.Flags().Bool("flush", false, "") cmd.Flags().Bool("flush", false, "")
cmd.Flags().StringP("input", "i", "/dev/stdin", "") cmd.Flags().StringP("input", "i", "/dev/stdin", "")
original, err := os.ReadFile(root + "/test.yaml") original, err := os.ReadFile(testdata.YAML("test"))
if err != nil { if err != nil {
t.Fatalf("could not read file") t.Fatalf("could not read file")
} }

View File

@ -8,7 +8,7 @@ import (
"testing" "testing"
opclient "git.rob.mx/nidito/joao/internal/op-client" opclient "git.rob.mx/nidito/joao/internal/op-client"
fixtures "git.rob.mx/nidito/joao/internal/testing" "git.rob.mx/nidito/joao/internal/testdata"
"github.com/1Password/connect-sdk-go/onepassword" "github.com/1Password/connect-sdk-go/onepassword"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
@ -76,9 +76,9 @@ func TestDeprecatedUpdate(t *testing.T) {
return *bytes.NewBufferString("updated"), nil return *bytes.NewBufferString("updated"), nil
} }
original := fieldify(fixtures.NewTestConfig("some:test")) original := fieldify(testdata.NewTestConfig("some:test"))
updated := fieldify(fixtures.NewTestConfig(original.Title)) updated := fieldify(testdata.NewTestConfig(original.Title))
updated.Fields[2].Value = "42" updated.Fields[2].Value = "42"
updated.Fields[4].Value = "42" updated.Fields[4].Value = "42"
@ -104,7 +104,7 @@ func TestDeprecatedUpdate(t *testing.T) {
} }
func TestUpdate(t *testing.T) { func TestUpdate(t *testing.T) {
fixtures.EnableDebugLogging() testdata.EnableDebugLogging()
client := &opclient.CLI{} client := &opclient.CLI{}
queriedVersion := false queriedVersion := false
var calledArgs []string var calledArgs []string
@ -123,9 +123,9 @@ func TestUpdate(t *testing.T) {
return *bytes.NewBufferString("updated"), nil return *bytes.NewBufferString("updated"), nil
} }
original := fieldify(fixtures.NewTestConfig("some:test")) original := fieldify(testdata.NewTestConfig("some:test"))
updated := fieldify(fixtures.NewTestConfig(original.Title)) updated := fieldify(testdata.NewTestConfig(original.Title))
updated.Fields[2].Value = "42" updated.Fields[2].Value = "42"
updated.Fields[4].Value = "42" updated.Fields[4].Value = "42"

View File

@ -1,10 +1,51 @@
package fixtures package testdata
import ( import (
"fmt"
"os"
"path"
"runtime"
"testing"
opclient "git.rob.mx/nidito/joao/internal/op-client"
"git.rob.mx/nidito/joao/internal/testdata/opconnect"
"github.com/1Password/connect-sdk-go/connect"
"github.com/1Password/connect-sdk-go/onepassword" "github.com/1Password/connect-sdk-go/onepassword"
"github.com/sirupsen/logrus" "github.com/sirupsen/logrus"
) )
func MockOPConnect(t *testing.T) {
t.Helper()
opclient.ConnectClientFactory = func(host, token, userAgent string) connect.Client {
return &opconnect.Client{}
}
client := opclient.NewConnect("", "")
opclient.Use(client)
opconnect.Clear()
}
func FromProjectRoot() string {
_, filename, _, _ := runtime.Caller(0)
dir := path.Join(path.Dir(filename), "../")
if err := os.Chdir(dir); err != nil {
panic(err)
}
wd, _ := os.Getwd()
return wd
}
func TempDir(t *testing.T, name string) string {
newDir, err := os.MkdirTemp("", name+"-*")
if err != nil {
t.Fatalf("could not create tempdir")
}
return newDir
}
func YAML(name string) string {
return path.Join(FromProjectRoot(), "testdata", fmt.Sprintf("%s.yaml", name))
}
func EnableDebugLogging() { func EnableDebugLogging() {
logrus.SetLevel(logrus.DebugLevel) logrus.SetLevel(logrus.DebugLevel)
} }
@ -208,5 +249,3 @@ func NewTestConfig(title string) *onepassword.Item {
}, },
} }
} }
var TestConfig = NewTestConfig("some:test")

View File

@ -1,6 +1,6 @@
// Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx> // Copyright © 2022 Roberto Hidalgo <joao@un.rob.mx>
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package mock package opconnect
import ( import (
"fmt" "fmt"

View File

@ -6,7 +6,7 @@ import (
"context" "context"
"testing" "testing"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
"git.rob.mx/nidito/joao/internal/vault" "git.rob.mx/nidito/joao/internal/vault"
"git.rob.mx/nidito/joao/internal/vault/middleware" "git.rob.mx/nidito/joao/internal/vault/middleware"
"github.com/1Password/connect-sdk-go/connect" "github.com/1Password/connect-sdk-go/connect"
@ -15,7 +15,7 @@ import (
func init() { func init() {
vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) { vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) {
return &mock.Client{}, nil return &opconnect.Client{}, nil
} }
} }
@ -32,15 +32,15 @@ func TestConfiguredBackend(t *testing.T) {
t.Fatalf("Unexpected error with config set: %s => %v", err, resp) t.Fatalf("Unexpected error with config set: %s => %v", err, resp)
} }
if resp.Data["token"] != mock.Token { if resp.Data["token"] != opconnect.Token {
t.Errorf("Found unknown token: %s", resp.Data["token"]) t.Errorf("Found unknown token: %s", resp.Data["token"])
} }
if resp.Data["host"] != mock.Host { if resp.Data["host"] != opconnect.Host {
t.Errorf("Found unknown host: %s", resp.Data["host"]) t.Errorf("Found unknown host: %s", resp.Data["host"])
} }
if resp.Data["vault"] != mock.Vaults[0].ID { if resp.Data["vault"] != opconnect.Vaults[0].ID {
t.Errorf("Found unknown vault: %s", resp.Data["vault"]) t.Errorf("Found unknown vault: %s", resp.Data["vault"])
} }
} }

View File

@ -6,7 +6,7 @@ import (
"context" "context"
"testing" "testing"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
"github.com/hashicorp/vault/sdk/logical" "github.com/hashicorp/vault/sdk/logical"
) )
@ -49,7 +49,7 @@ func TestConfigDefault(t *testing.T) {
t.Fatal("got no response, expected something!") t.Fatal("got no response, expected something!")
} }
mapsEqual(t, resp.Data, map[string]any{"host": mock.Host, "token": mock.Token, "vault": mock.Vaults[0].ID}) mapsEqual(t, resp.Data, map[string]any{"host": opconnect.Host, "token": opconnect.Token, "vault": opconnect.Vaults[0].ID})
} }
func TestConfigUpdate(t *testing.T) { func TestConfigUpdate(t *testing.T) {

View File

@ -8,7 +8,7 @@ import (
"testing" "testing"
"time" "time"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
"git.rob.mx/nidito/joao/internal/vault" "git.rob.mx/nidito/joao/internal/vault"
"git.rob.mx/nidito/joao/internal/vault/middleware" "git.rob.mx/nidito/joao/internal/vault/middleware"
"github.com/1Password/connect-sdk-go/connect" "github.com/1Password/connect-sdk-go/connect"
@ -44,7 +44,7 @@ func getBackend(tb testing.TB) (logical.Backend, logical.Storage) {
cfg := testConfig() cfg := testConfig()
ctx := context.Background() ctx := context.Background()
data, err := json.Marshal(map[string]string{"host": mock.Host, "token": mock.Token, "vault": mock.Vaults[0].ID}) data, err := json.Marshal(map[string]string{"host": opconnect.Host, "token": opconnect.Token, "vault": opconnect.Vaults[0].ID})
if err != nil { if err != nil {
tb.Fatalf("Could not serialize config for client: %s", err) tb.Fatalf("Could not serialize config for client: %s", err)
} }
@ -77,6 +77,6 @@ func getUnconfiguredBackend(tb testing.TB) (logical.Backend, logical.Storage) {
func setOnePassswordConnectMocks() { func setOnePassswordConnectMocks() {
vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) { vault.ConnectClientFactory = func(s logical.Storage) (connect.Client, error) {
return &mock.Client{}, nil return &opconnect.Client{}, nil
} }
} }

View File

@ -8,7 +8,7 @@ import (
"fmt" "fmt"
"testing" "testing"
"git.rob.mx/nidito/joao/internal/op-client/mock" "git.rob.mx/nidito/joao/internal/testdata/opconnect"
"github.com/1Password/connect-sdk-go/onepassword" "github.com/1Password/connect-sdk-go/onepassword"
"github.com/hashicorp/vault/sdk/logical" "github.com/hashicorp/vault/sdk/logical"
) )
@ -20,8 +20,8 @@ func getTestBackendWithConfig(t *testing.T) (logical.Backend, logical.Storage) {
func TestReadEntry(t *testing.T) { func TestReadEntry(t *testing.T) {
b, reqStorage := getTestBackendWithConfig(t) b, reqStorage := getTestBackendWithConfig(t)
mock.Clear() opconnect.Clear()
item := mock.Add(generateConfigItem("service:test")) item := opconnect.Add(generateConfigItem("service:test"))
expected := map[string]any{ expected := map[string]any{
"boolean": false, "boolean": false,
"integer": 42, "integer": 42,
@ -89,8 +89,8 @@ func TestReadEntry(t *testing.T) {
func TestListEntries(t *testing.T) { func TestListEntries(t *testing.T) {
b, reqStorage := getTestBackendWithConfig(t) b, reqStorage := getTestBackendWithConfig(t)
mock.Clear() opconnect.Clear()
item := mock.Add(generateConfigItem("service:test")) item := opconnect.Add(generateConfigItem("service:test"))
expected := map[string]any{ expected := map[string]any{
"keys": []string{ "keys": []string{
@ -174,7 +174,7 @@ func generateConfigItem(title string) *onepassword.Item {
Category: "password", Category: "password",
Title: title, Title: title,
Vault: onepassword.ItemVault{ Vault: onepassword.ItemVault{
ID: mock.Vaults[0].ID, ID: opconnect.Vaults[0].ID,
}, },
Fields: []*onepassword.ItemField{ Fields: []*onepassword.ItemField{
{ {