From 67e36d44585f805a27a0148c2cd789d999f37af1 Mon Sep 17 00:00:00 2001 From: Yo'av Moshe Date: Sat, 21 Sep 2024 17:05:08 +0200 Subject: [PATCH] add tests --- go.mod | 5 +++ go.sum | 9 +++++ parsers/utils_test.go | 78 +++++++++++++++++++++++++++++++++++++++++++ utils/utils_test.go | 34 +++++++++++++++++++ 4 files changed, 126 insertions(+) create mode 100644 parsers/utils_test.go create mode 100644 utils/utils_test.go diff --git a/go.mod b/go.mod index 16f010f..d3a0152 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ require ( github.com/PuerkitoBio/goquery v1.9.2 github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 github.com/playwright-community/playwright-go v0.4501.1 + github.com/stretchr/testify v1.8.4 github.com/tidwall/gjson v1.17.3 github.com/urfave/cli/v2 v2.27.4 ) @@ -13,9 +14,12 @@ require ( require ( github.com/andybalholm/cascadia v1.3.2 // indirect github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect + github.com/davecgh/go-spew v1.1.1 // indirect github.com/deckarep/golang-set/v2 v2.6.0 // indirect github.com/go-jose/go-jose/v3 v3.0.3 // indirect github.com/go-stack/stack v1.8.1 // indirect + github.com/kr/text v0.2.0 // indirect + github.com/pmezard/go-difflib v1.0.0 // indirect github.com/russross/blackfriday/v2 v2.1.0 // indirect github.com/tidwall/match v1.1.1 // indirect github.com/tidwall/pretty v1.2.1 // indirect @@ -23,4 +27,5 @@ require ( go.uber.org/multierr v1.11.0 // indirect golang.org/x/exp v0.0.0-20240823005443-9b4947da3948 // indirect golang.org/x/net v0.24.0 // indirect + gopkg.in/yaml.v3 v3.0.1 // indirect ) diff --git a/go.sum b/go.sum index a133679..bca1b0f 100644 --- a/go.sum +++ b/go.sum @@ -4,6 +4,7 @@ github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsVi github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU= github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4= github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= +github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= @@ -16,12 +17,18 @@ github.com/go-stack/stack v1.8.1/go.mod h1:dcoOX6HbPZSZptuspn9bctJ+N/CnF5gGygcUP github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 h1:El6M4kTTCOh6aBiKaUGG7oYTSPP8MxqL4YI3kZKwcP4= github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510/go.mod h1:pupxD2MaaD3pAXIBCelhxNneeOaAeabZDe5s4K6zSpQ= +github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE= +github.com/kr/pretty v0.3.1/go.mod h1:hoEshYVHaxMs3cyo3Yncou5ZscifuDolrwPKZanG3xk= +github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY= +github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE= github.com/mitchellh/go-ps v1.0.0 h1:i6ampVEEF4wQFF+bkYfwYgY+F/uYJDktmvLPf7qIgjc= github.com/mitchellh/go-ps v1.0.0/go.mod h1:J4lOc8z8yJs6vUwklHw2XEIiT4z4C40KtWVN3nvg8Pg= github.com/playwright-community/playwright-go v0.4501.1 h1:kz8SIfR6nEI8blk77nTVD0K5/i37QP5rY/o8a1fG+4c= github.com/playwright-community/playwright-go v0.4501.1/go.mod h1:bpArn5TqNzmP0jroCgw4poSOG9gSeQg490iLqWAaa7w= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/rogpeppe/go-internal v1.11.0 h1:cWPaGQEPrBb5/AsnsZesgZZ9yb1OQ+GOISoDNXVBh4M= +github.com/rogpeppe/go-internal v1.11.0/go.mod h1:ddIwULY96R17DhadqLgMfk9H9tvdUzkipdSkR5nkCZA= github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk= github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= @@ -87,6 +94,8 @@ golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc golang.org/x/tools v0.6.0/go.mod h1:Xwgl3UAJ/d3gWutnCtw505GrjyAbvKui8lOU390QaIU= golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c h1:Hei/4ADfdWqJk1ZMxUNpqntNwaWcugrBjAiHlqqRiVk= +gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EVd6muEfDQjcINNoR0C8j2r3qZ4Q= gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/parsers/utils_test.go b/parsers/utils_test.go new file mode 100644 index 0000000..b6ab15b --- /dev/null +++ b/parsers/utils_test.go @@ -0,0 +1,78 @@ +package parsers + +import ( + "fmt" + "os" + "os/exec" + "testing" + + "github.com/bjesus/pipet/common" + "github.com/stretchr/testify/assert" +) + +// Mock exec.Command for testing ExecuteCurlBlock and ExecutePipe +var execCommand = exec.Command + +func mockExecCommand(command string, args ...string) *exec.Cmd { + cs := []string{"-test.run=TestHelperProcess", "--", command} + cs = append(cs, args...) + cmd := exec.Command(os.Args[0], cs...) + cmd.Env = []string{"GO_WANT_HELPER_PROCESS=1"} + return cmd +} + +// Test helper for mock exec +func TestHelperProcess(*testing.T) { + if os.Getenv("GO_WANT_HELPER_PROCESS") != "1" { + return + } + cmd := os.Args[3] + switch cmd { + case "curl": + os.Stdout.Write([]byte(`{"success":true}`)) // Mock JSON response + default: + os.Stdout.Write([]byte("bash output")) + } + os.Exit(0) +} + +// Test ExecuteCurlBlock +func TestExecuteCurlBlock(t *testing.T) { + execCommand = mockExecCommand + defer func() { execCommand = exec.Command }() // Restore exec.Command after test + + block := common.Block{ + Type: "curl", + Command: "curl http://example.com", + Queries: []string{"h1"}, + } + result, _, err := ExecuteCurlBlock(block) + fmt.Printf("%v", result) + assert.NoError(t, err) + assert.NotNil(t, result) + assert.Equal(t, []interface{}{"Example Domain"}, result) // Mocked JSON output +} + +// Test ExecutePipe +func TestExecutePipe(t *testing.T) { + execCommand = mockExecCommand + defer func() { execCommand = exec.Command }() // Restore exec.Command after test + + input := "pipet" + command := "wc -c" + output, err := ExecutePipe(input, command) + assert.NoError(t, err) + assert.Equal(t, "5\n", output) // Mocked output +} + +// Test CalculateIndentation +func TestCalculateIndentation(t *testing.T) { + assert.Equal(t, 4, CalculateIndentation(" indented")) + assert.Equal(t, 0, CalculateIndentation("not indented")) +} + +// Test commandExists +func TestCommandExists(t *testing.T) { + assert.True(t, commandExists("bash")) // Assuming bash exists on your system + assert.False(t, commandExists("fake")) // Fake command +} diff --git a/utils/utils_test.go b/utils/utils_test.go new file mode 100644 index 0000000..2e65663 --- /dev/null +++ b/utils/utils_test.go @@ -0,0 +1,34 @@ +package utils + +import ( + "os" + "testing" + + "github.com/bjesus/pipet/common" + "github.com/stretchr/testify/assert" // Or use default `testing` if no external dependency needed +) + +// Test FlattenData +func TestFlattenData(t *testing.T) { + data := []interface{}{"a", "b", []interface{}{"c", "d"}} + expected := []string{"a", "b", "c", "d"} + result := FlattenData(data, 0) + assert.Equal(t, expected, result) +} + +// Test GetSeparator +func TestGetSeparator(t *testing.T) { + app := &common.PipetApp{ + Separator: []string{"-", ":"}, + } + assert.Equal(t, "-", GetSeparator(app, 0)) + assert.Equal(t, ", ", GetSeparator(app, 5)) // Test default case +} + +// Test FileExists +func TestFileExists(t *testing.T) { + f, _ := os.CreateTemp("", "example") + defer os.Remove(f.Name()) // Cleanup + assert.True(t, FileExists(f.Name())) + assert.False(t, FileExists("nonexistent.file")) +}