-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Christopher Phillips <[email protected]>
- Loading branch information
Showing
4 changed files
with
90 additions
and
13 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package cli | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"os/exec" | ||
"testing" | ||
) | ||
|
||
const grantTmpPath = "../../.tmp/grant" | ||
|
||
func buildBinary() (string, error) { | ||
buildCmd := exec.Command("go", "build", "-o", grantTmpPath, "../../cmd/grant/main.go") // Adjust the last argument to your package path if necessary | ||
err := buildCmd.Run() | ||
return grantTmpPath, err | ||
} | ||
|
||
// setup function that you want to run before any tests | ||
func setup(m *testing.M) { | ||
_, err := buildBinary() | ||
if err != nil { | ||
log.Fatalf("Failed to build binary: %v", err) | ||
} | ||
} | ||
|
||
// teardown function to clean up after the tests | ||
func teardown() { | ||
// Your cleanup code here | ||
println("Running teardown after all tests.") | ||
} | ||
|
||
// TestMain is the entry point for testing | ||
func TestMain(m *testing.M) { | ||
setup(m) // Call setup | ||
code := m.Run() // Run the tests and store the result | ||
teardown() // Call teardown | ||
os.Exit(code) // Exit with the result of the tests | ||
} |
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 |
---|---|---|
@@ -1 +1,12 @@ | ||
package cli | ||
|
||
import "os" | ||
|
||
func fileExists(filename string) bool { | ||
info, err := os.Stat(filename) | ||
if os.IsNotExist(err) { | ||
return false | ||
} | ||
// We also check if the file might actually be a directory. | ||
return !info.IsDir() | ||
} |
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 |
---|---|---|
@@ -1,29 +1,53 @@ | ||
package cli | ||
|
||
import "testing" | ||
import ( | ||
"os/exec" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// Note main_test.go is used to set up and teardown the tests. This is the entry point for testing and | ||
// responsible for building the most recent version of the grant binary. | ||
func Test_VersionCommand(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
command string | ||
name string | ||
command string | ||
expectedInOutput []string | ||
}{ | ||
{ | ||
name: "text output", | ||
command: "version", | ||
name: "text output", | ||
command: "--version", | ||
expectedInOutput: []string{"[not provided]"}, | ||
}, | ||
{ | ||
name: "json output", | ||
command: "version -o json", | ||
}, | ||
{ | ||
name: "root command short version output", | ||
command: "--version", | ||
name: "long form", | ||
command: "version", | ||
expectedInOutput: []string{ | ||
"Application:", | ||
"Version:", | ||
"BuildDate:", | ||
"GitCommit:", | ||
"GitDescription:", | ||
"Platform:", | ||
"GoVersion:", | ||
"Compiler:", | ||
}, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
// check if the command is available | ||
cmd := exec.Command(grantTmpPath, test.command) | ||
output, err := cmd.CombinedOutput() | ||
if err != nil { | ||
t.Fatalf("command failed: %v: cmd output: %s", err, string(output)) | ||
} | ||
|
||
for _, expected := range test.expectedInOutput { | ||
assert.Contains(t, string(output), expected, "expected output: %s not found in command output: %s", expected, string(output)) | ||
} | ||
}) | ||
} | ||
} |