-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cmd): add support for cmd (#11)
- Loading branch information
1 parent
b6312a4
commit 123e2e9
Showing
4 changed files
with
244 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 main | ||
|
||
import ( | ||
"flag" | ||
"fmt" | ||
"io" | ||
"os" | ||
|
||
"github.com/gkampitakis/ciinfo" | ||
) | ||
|
||
const ( | ||
YES = 0 | ||
NO = -1 | ||
) | ||
|
||
func main() { | ||
isPr := flag.Bool("pr", false, "check if shell is running on CI for a Pull Request.") | ||
output := flag.String("output", "", "you can output info [json, pretty].") | ||
|
||
flag.Parse() | ||
|
||
run(os.Exit, os.Stdout, *isPr, *output) | ||
} | ||
|
||
func run(exit func(int), w io.Writer, isPR bool, output string) { | ||
result := NO | ||
|
||
if isPR { | ||
if ciinfo.IsPr { | ||
result = YES | ||
} | ||
|
||
exit(result) | ||
} | ||
|
||
if output == "json" { | ||
jsonPrint(w) | ||
return | ||
} | ||
|
||
if output == "pretty" { | ||
prettyPrint(w) | ||
return | ||
} | ||
|
||
if ciinfo.IsCI { | ||
result = YES | ||
} | ||
|
||
exit(result) | ||
} | ||
|
||
func jsonPrint(w io.Writer) { | ||
fmt.Fprintf(w, `{ | ||
"is_ci": %t, | ||
"ci_name": "%s", | ||
"pull_request": %t | ||
} | ||
`, ciinfo.IsCI, ciinfo.Name, ciinfo.IsPr) | ||
} | ||
|
||
func prettyPrint(w io.Writer) { | ||
if !ciinfo.IsCI { | ||
fmt.Fprintln(w, "Not running on CI.") | ||
return | ||
} | ||
|
||
if ciinfo.Name != "" { | ||
fmt.Fprintf(w, "CI Name: %s\n", ciinfo.Name) | ||
} else { | ||
fmt.Fprintln(w, "Running on CI.") | ||
} | ||
|
||
if ciinfo.IsPr { | ||
fmt.Fprintln(w, "Is Pull Request.") | ||
} | ||
} |
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,137 @@ | ||
package main | ||
|
||
import ( | ||
"io" | ||
"strings" | ||
"testing" | ||
|
||
"github.com/gkampitakis/ciinfo" | ||
) | ||
|
||
func mockExit(t *testing.T, expectedCode int) func(int) { | ||
t.Helper() | ||
return func(i int) { | ||
if i != expectedCode { | ||
t.Errorf("expected code: %d but got: %d\n", expectedCode, i) | ||
} | ||
} | ||
} | ||
|
||
type mockWriter struct { | ||
mockWrite func([]byte) | ||
} | ||
|
||
func (m mockWriter) Write(p []byte) (int, error) { | ||
m.mockWrite(p) | ||
return 0, nil | ||
} | ||
|
||
func TestCMD(t *testing.T) { | ||
t.Run("should be successful in case of pr", func(t *testing.T) { | ||
ciinfo.IsPr = true | ||
|
||
run(mockExit(t, 0), io.Discard, true, "") | ||
}) | ||
|
||
t.Run("should exit with error in case not being a pr", func(t *testing.T) { | ||
ciinfo.IsPr = false | ||
ciinfo.IsCI = false | ||
|
||
run(mockExit(t, -1), io.Discard, true, "") | ||
}) | ||
|
||
t.Run("should print json output", func(t *testing.T) { | ||
ciinfo.IsPr = true | ||
ciinfo.Name = "mock-ci" | ||
ciinfo.IsCI = true | ||
|
||
m := mockWriter{ | ||
mockWrite: func(b []byte) { | ||
// clean up tabs,spaces and newlines | ||
chars := []string{" ", "\n", "\t"} | ||
v := string(b) | ||
for _, c := range chars { | ||
v = strings.ReplaceAll(v, c, "") | ||
} | ||
|
||
if v != `{"is_ci":true,"ci_name":"mock-ci","pull_request":true}` { | ||
t.Error("expected json format") | ||
} | ||
}, | ||
} | ||
|
||
run(nil, m, false, "json") | ||
}) | ||
|
||
t.Run("should print pretty output when not on ci", func(t *testing.T) { | ||
ciinfo.IsCI = false | ||
|
||
m := mockWriter{ | ||
mockWrite: func(b []byte) { | ||
if string(b) != "Not running on CI.\n" { | ||
t.Error("expected not running on CI.") | ||
} | ||
}, | ||
} | ||
|
||
run(nil, m, false, "pretty") | ||
}) | ||
|
||
t.Run("should print pretty output", func(t *testing.T) { | ||
ciinfo.IsPr = true | ||
ciinfo.Name = "mock-ci" | ||
ciinfo.IsCI = true | ||
msgs := []string{ | ||
"CI Name: mock-ci\n", | ||
"Is Pull Request.\n", | ||
} | ||
|
||
m := mockWriter{ | ||
mockWrite: func(b []byte) { | ||
msg := msgs[0] | ||
msgs = msgs[1:] | ||
if string(b) != msg { | ||
t.Errorf("unexpected message: %s", string(b)) | ||
} | ||
}, | ||
} | ||
|
||
run(nil, m, false, "pretty") | ||
}) | ||
|
||
t.Run("should print pretty output without name", func(t *testing.T) { | ||
ciinfo.IsPr = true | ||
ciinfo.Name = "" | ||
ciinfo.IsCI = true | ||
msgs := []string{ | ||
"Running on CI.\n", | ||
"Is Pull Request.\n", | ||
} | ||
|
||
m := mockWriter{ | ||
mockWrite: func(b []byte) { | ||
msg := msgs[0] | ||
msgs = msgs[1:] | ||
if string(b) != msg { | ||
t.Errorf("unexpected message: %s", string(b)) | ||
} | ||
}, | ||
} | ||
|
||
run(nil, m, false, "pretty") | ||
}) | ||
|
||
t.Run("should be successful code", func(t *testing.T) { | ||
ciinfo.IsPr = true | ||
ciinfo.Name = "mock-ci" | ||
ciinfo.IsCI = true | ||
|
||
run(mockExit(t, 0), io.Discard, false, "") | ||
}) | ||
|
||
t.Run("should be error code", func(t *testing.T) { | ||
ciinfo.IsCI = false | ||
|
||
run(mockExit(t, -1), io.Discard, false, "") | ||
}) | ||
} |