-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
126 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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")) | ||
} |