-
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.
- Loading branch information
0 parents
commit d92c5d7
Showing
4 changed files
with
131 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module github.com/hmarf/ctest | ||
|
||
go 1.16 | ||
|
||
require ( | ||
github.com/fatih/color v1.10.0 | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 | ||
) |
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,12 @@ | ||
github.com/fatih/color v1.10.0 h1:s36xzo75JdqLaaWoiEHk767eHiwo0598uUxyfiPkDsg= | ||
github.com/fatih/color v1.10.0/go.mod h1:ELkj/draVOlAH/xkhN6mQ50Qd0MPOk5AAr3maGEBuJM= | ||
github.com/mattn/go-colorable v0.1.8 h1:c1ghPdyEDarC70ftn0y+A/Ee++9zz8ljHG1b13eJ0s8= | ||
github.com/mattn/go-colorable v0.1.8/go.mod h1:u6P/XSegPjTcexA+o6vUJrdnUu04hMope9wVRipJSqc= | ||
github.com/mattn/go-isatty v0.0.12 h1:wuysRhFDzyxgEmMf5xjvJ2M9dZoWAXNNr5LSBS7uHXY= | ||
github.com/mattn/go-isatty v0.0.12/go.mod h1:cbi8OIDigv2wuxKPP5vlRcQ1OAZbq2CE4Kysco4FUpU= | ||
golang.org/x/sys v0.0.0-20200116001909-b77594299b42/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20200223170610-d5e6a3e2c0ae/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68 h1:nxC68pudNYkKU6jWhgrqdreuFiOQWj1Fs7T3VrH4Pjw= | ||
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1 h1:v+OssWQX+hTHEmOBgwxdZxK4zHq3yOs8F9J7mk0PY8E= | ||
golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= |
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,85 @@ | ||
package main | ||
|
||
import ( | ||
"bufio" | ||
"errors" | ||
"fmt" | ||
"os" | ||
"strings" | ||
|
||
"github.com/fatih/color" | ||
|
||
"golang.org/x/term" | ||
) | ||
|
||
func main() { | ||
if err := cTest(); err != nil { | ||
fmt.Println(err) | ||
} | ||
} | ||
|
||
func cTest() error { | ||
|
||
if !term.IsTerminal(int(os.Stdin.Fd())) { | ||
if err := readLines(os.Stdin); err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
return errors.New("must be pipe input") | ||
} | ||
|
||
// reference: https://www.yunabe.jp/tips/golang_readlines.html | ||
func readLines(f *os.File) error { | ||
s := bufio.NewScanner(f) | ||
for s.Scan() { | ||
colorString(s.Text()) | ||
} | ||
if s.Err() != nil { | ||
return s.Err() | ||
} | ||
return nil | ||
} | ||
|
||
var c *color.Color | ||
var ( | ||
success = color.New(color.FgGreen) | ||
fail = color.New(color.FgHiRed) | ||
) | ||
|
||
func colorString(line string) { | ||
trimmed := strings.TrimSpace(line) | ||
switch { | ||
case strings.HasPrefix(trimmed, "=== RUN"): | ||
fallthrough | ||
case strings.HasPrefix(trimmed, "?"): | ||
c = nil | ||
|
||
// success | ||
case strings.HasPrefix(trimmed, "--- PASS"): | ||
fallthrough | ||
case strings.HasPrefix(trimmed, "ok"): | ||
fallthrough | ||
case strings.HasPrefix(trimmed, "PASS"): | ||
c = success | ||
|
||
// failure | ||
case strings.HasPrefix(trimmed, "--- FAIL"): | ||
fallthrough | ||
case strings.HasPrefix(trimmed, "FAIL"): | ||
c = fail | ||
} | ||
|
||
if c == nil { | ||
fmt.Printf("%s\n", line) | ||
return | ||
} | ||
c.Printf("%s\n", line) | ||
} | ||
|
||
func Example(code string) (int, error) { | ||
if code == "hoge" { | ||
return 1, nil | ||
} | ||
return 0, errors.New("code must be hoge") | ||
} |
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,26 @@ | ||
package main | ||
|
||
import ( | ||
"testing" // テストで使える関数・構造体が用意されているパッケージをimport | ||
) | ||
|
||
func TestExampleSuccess(t *testing.T) { | ||
result, err := Example("hoge") | ||
if err != nil { | ||
t.Fatalf("failed test %#v", err) | ||
} | ||
if result != 1 { | ||
t.Fatal("failed test") | ||
} | ||
} | ||
|
||
func TestExampleFailed(t *testing.T) { | ||
result, err := Example("fuga") | ||
if err == nil { | ||
t.Fatal("failed test") | ||
} | ||
if result != 0 { | ||
t.Fatal("failed test") | ||
} | ||
// time.Sleep(time.Second * 10) | ||
} |