Skip to content

Commit

Permalink
Merge pull request #6 from k1LoW/windows-ci
Browse files Browse the repository at this point in the history
Refactor code
  • Loading branch information
k1LoW authored Nov 30, 2019
2 parents ad6436f + d4efede commit aa4de85
Show file tree
Hide file tree
Showing 8 changed files with 131 additions and 59 deletions.
16 changes: 15 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,42 @@ on: push
jobs:
job-test:
name: Test
runs-on: ubuntu-latest
strategy:
matrix:
go_version: [1.12, 1.13]
platform: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.platform }}
steps:
- name: Set up Go ${{ matrix.go_version }}
uses: actions/setup-go@v1
with:
go-version: ${{ matrix.go_version }}

- name: Install codecov
if: matrix.platform == 'ubuntu-latest'
run: sudo pip install codecov

- name: Check out source code
uses: actions/checkout@v1

- name: Test
run: env PATH=`go env GOPATH`/bin:$PATH make ci
if: matrix.platform != 'windows-latest'
env:
GOPROXY: "https://proxy.golang.org"

- name: Test
run: |
go test ./...
go test ./... -tags integration
shell: cmd
if: matrix.platform == 'windows-latest'
env:
GO111MODULE: on
GOPROXY: "https://proxy.golang.org"

- name: Run codecov
if: matrix.platform == 'ubuntu-latest'
run: codecov
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,14 @@ BUILD_LDFLAGS = -X $(PKG).commit=$(COMMIT) -X $(PKG).date=$(DATE)

default: test

ci: depsdev test sec
ci: depsdev test test_integration sec

test:
go test ./... -coverprofile=coverage.txt -covermode=count

test_integration: build
go test ./... -tags integration

sec:
gosec ./...

Expand Down
8 changes: 5 additions & 3 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,11 @@ var rootCmd = &cobra.Command{
defer cancel()

if erase {
e := eraser.NewEraser()
for o := range e.Handle(ctx, os.Stdin) {
fmt.Fprintf(os.Stdout, "%s", o)
e := eraser.NewEraser(os.Stdout)
err := e.Handle(ctx, os.Stdin)
if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
} else {
p := painter.NewPainter(args)
Expand Down
54 changes: 23 additions & 31 deletions eraser/eraser.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,49 +3,41 @@ package eraser
import (
"bufio"
"context"
"fmt"
"io"
"os"

"github.com/acarl005/stripansi"
"github.com/mattn/go-colorable"
)

type Eraser struct {
out chan string
out io.Writer
}

func NewEraser() *Eraser {
func NewEraser(out io.Writer) *Eraser {
return &Eraser{
out: make(chan string),
out: out,
}
}

func (e *Eraser) Erase(s string) string {
return stripansi.Strip(s)
}

func (e *Eraser) Handle(ctx context.Context, inn io.Reader) <-chan string {
func (e *Eraser) Handle(ctx context.Context, inn io.Reader) error {
in := bufio.NewReader(inn)

go func() {
defer close(e.out)
L:
for {
s, err := in.ReadString('\n')
if err == io.EOF {
break
} else if err != nil {
_, _ = fmt.Fprintf(os.Stderr, "%s\n", err)
os.Exit(1)
}
select {
case <-ctx.Done():
break L
default:
e.out <- e.Erase(s)
out := colorable.NewNonColorable(e.out)

for {
s, err := in.ReadBytes('\n')
if err == io.EOF {
break
} else if err != nil {
return err
}
select {
case <-ctx.Done():
break
default:
_, err = out.Write(s)
if err != nil {
return nil
}
}
}()

return e.out
}
return nil
}
27 changes: 22 additions & 5 deletions eraser/eraser_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package eraser

import "testing"
import (
"bytes"
"context"
"fmt"
"testing"
)

func TestErase(t *testing.T) {
var tests = []struct {
Expand All @@ -12,11 +17,23 @@ func TestErase(t *testing.T) {
{"\x1b[31;1mHe\x1b[0ml\x1b[36;1mlo\x1b[0m", "Hello"},
}

e := NewEraser()
for _, tt := range tests {
got := e.Erase(tt.in)
if tt.want != got {
t.Errorf("Erase(): got = %v ,want = %v", got, tt.want)
stdin := &bytes.Buffer{}
stdout := &bytes.Buffer{}
ctx := context.Background()
e := NewEraser(stdout)
stdin.WriteString(fmt.Sprintf("%s\n", tt.in))
err := e.Handle(ctx, stdin)
if err != nil {
t.Fatal(err)
}
got, err := stdout.ReadString('\n')
if err != nil {
t.Fatal(err)
}
want := fmt.Sprintf("%s\n", tt.want)
if want != got {
t.Errorf("Paint(): got = %#v ,want = %#v", got, want)
}
}
}
10 changes: 4 additions & 6 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
module github.com/k1LoW/colr

go 1.12
go 1.13

require (
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d
github.com/labstack/gommon v0.2.9
github.com/mattn/go-colorable v0.1.2
github.com/mattn/go-isatty v0.0.8
github.com/labstack/gommon v0.3.0
github.com/mattn/go-colorable v0.1.4
github.com/mattn/go-isatty v0.0.10
github.com/spf13/cobra v0.0.5
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 // indirect
)
25 changes: 13 additions & 12 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d h1:licZJFw2RwpHMqeKTCYkitsPqHNxTmd4SNR5r94FGM8=
github.com/acarl005/stripansi v0.0.0-20180116102854-5a71ef0e047d/go.mod h1:asat636LX7Bqt5lYEZ27JNDcqxfjdBQuJ/MM4CN/Lzo=
github.com/armon/consul-api v0.0.0-20180202201655-eb2c6b5be1b6/go.mod h1:grANhF5doyWs3UAsr3K4I6qtAmlQcZDesFNEHPZAzj8=
github.com/coreos/etcd v3.3.10+incompatible/go.mod h1:uF7uidLiAD3TWHmW31ZFd/JWoc32PjwdhPthX9715RE=
github.com/coreos/go-etcd v2.0.0+incompatible/go.mod h1:Jez6KQU2B/sWsbdaef3ED8NzMklzPG4d5KIOhIy30Tk=
Expand All @@ -13,13 +11,16 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/hashicorp/hcl v1.0.0/go.mod h1:E5yfLk+7swimpb2L/Alb/PJmXilQ/rhwaUYs4T20WEQ=
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM=
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8=
github.com/labstack/gommon v0.2.9 h1:heVeuAYtevIQVYkGj6A41dtfT91LrvFG220lavpWhrU=
github.com/labstack/gommon v0.2.9/go.mod h1:E8ZTmW9vw5az5/ZyHWCp0Lw4OH2ecsaBP1C/NKavGG4=
github.com/labstack/gommon v0.3.0 h1:JEeO0bvc78PKdyHxloTKiF8BD5iGrH8T6MSeGvSgob0=
github.com/labstack/gommon v0.3.0/go.mod h1:MULnywXg0yavhxWKc+lOruYdAhDwPK9wf0OL7NoOu+k=
github.com/magiconair/properties v1.8.0/go.mod h1:PppfXfuXeibc/6YijjN8zIbojt8czPbwD3XqdrwzmxQ=
github.com/mattn/go-colorable v0.1.2 h1:/bC9yWikZXAL9uJdulbSfyVNIR3n3trXl+v8+1sx8mU=
github.com/mattn/go-colorable v0.1.2/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8 h1:HLtExJ+uU2HOZ+wI0Tt5DtUDrx8yhUqDcp7fYERX4CE=
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
github.com/mattn/go-isatty v0.0.9/go.mod h1:YNRxwqDuOph6SZLI9vUUz6OYw3QyUt7WiY2yME+cCiQ=
github.com/mattn/go-isatty v0.0.10 h1:qxFzApOv4WsAL965uUPIsXzAKCZxN2p9UqdhFS4ZW10=
github.com/mattn/go-isatty v0.0.10/go.mod h1:qgIWMr58cqv1PHHyhnkY9lrL7etaEgOFcMEpPG5Rm84=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/mapstructure v1.1.2/go.mod h1:FVVH3fgwuzCH5S8UJGiWEs2h04kUh9fWfEaFds41c1Y=
github.com/pelletier/go-toml v1.2.0/go.mod h1:5z9KED0ma1S8pY6P1sdut58dfprrGBbd/94hg7ilaic=
Expand All @@ -35,21 +36,21 @@ github.com/spf13/pflag v1.0.3 h1:zPAT6CGy6wXeQ7NtTnaTerfKOsV6V6F8agHXFiazDkg=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/spf13/viper v1.3.2/go.mod h1:ZiWeW+zYFKm7srdB9IoDzzZXaJaI5eL9QjNiN/DMA2s=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.2.0/go.mod h1:qt09Ya8vawLte6SNmTgCsAVtYtaKzEcn8ATUoHMkEqE=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
github.com/ugorji/go/codec v0.0.0-20181204163529-d75b2dcb6bc8/go.mod h1:VFNgLljTbGfSG7qAOspJ7OScBnGdDN/yBr0sguwnwf0=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.0.1/go.mod h1:UQGH1tvbgY+Nz5t2n7tXsz52dQxojPUpymEIMZ47gx8=
github.com/xordataexchange/crypt v0.0.3-0.20170626215501-b2862e3d0a77/go.mod h1:aYKd//L2LvnjZzWKhF00oedf4jCCReLcmhLdhm1A27Q=
golang.org/x/crypto v0.0.0-20181203042331-505ab145d0a9/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/sys v0.0.0-20181205085412-a5c9d58dba9a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190602015325-4c4f7f33c9ed/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7 h1:LepdCS8Gf/MVejFIt8lsiexZATdoGVyp5bcyS+rYoUI=
golang.org/x/sys v0.0.0-20190712062909-fae7ac547cb7/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190813064441-fde4db37ae7a/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191008105621-543471e840be h1:QAcqgptGM8IQBC9K/RC4o+O9YmqEm0diQn9QmZw/0mU=
golang.org/x/sys v0.0.0-20191008105621-543471e840be/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
45 changes: 45 additions & 0 deletions integration_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// +build integration

package main

import (
"os/exec"
"runtime"
"testing"
)

func init() {
err := exec.Command("go", "build").Run()
if err != nil {
panic(err)
}
}

func TestPaintAndErase(t *testing.T) {
var (
got []byte
want string
err error
)
if runtime.GOOS == "windows" {
got, err = exec.Command("cmd", "/c", `echo Hello| .\colr.exe He | .\colr.exe --erase`).Output()
want = "Hello\r\n"
} else {
got, err = exec.Command("bash", "-c", `echo Hello| ./colr He | ./colr --erase`).Output()
want = "Hello\n"
}
if err != nil {
t.Fatal(err)
}

if want != string(got) {
t.Errorf("got = %#v ,want = %#v", string(got), want)
}
}

type testcase struct {
name string
cmd []string
want string
processFinished bool
}

0 comments on commit aa4de85

Please sign in to comment.