From c8933c39bb8240f58b473d1edd2751132d81cb2a Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sat, 2 Apr 2022 17:54:25 +0900 Subject: [PATCH 1/7] Fix options to be not global --- cli/cli.go | 6 ++++-- cli/options.go | 2 -- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/cli/cli.go b/cli/cli.go index 39c4e0c..ba9ed3a 100644 --- a/cli/cli.go +++ b/cli/cli.go @@ -29,6 +29,8 @@ var ( ) func Run() (err error) { + var opts Options + parser := flags.NewParser(&opts, flags.HelpFlag|flags.PassDoubleDash) parser.Usage = fmt.Sprintf(Usage, name) @@ -98,10 +100,10 @@ func Run() (err error) { r = file } - return run(r, filename) + return run(opts, r, filename) } -func run(r io.Reader, filename string) error { +func run(opts Options, r io.Reader, filename string) error { var ( out io.ReadWriter err error diff --git a/cli/options.go b/cli/options.go index 8942602..c6bce71 100644 --- a/cli/options.go +++ b/cli/options.go @@ -13,5 +13,3 @@ type Options struct { NoWindowAccessBar bool `long:"no-window-access-bar" description:"Hide the window access bar"` ShowVersion bool `short:"v" long:"version" description:"Show version"` } - -var opts Options From 0d4580988b8f9e378e7fdb3c5d4e470e04f6b75f Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sat, 2 Apr 2022 18:27:38 +0900 Subject: [PATCH 2/7] Fix height value to be constant --- panel.go | 28 +++++++++++++++++++--------- 1 file changed, 19 insertions(+), 9 deletions(-) diff --git a/panel.go b/panel.go index 2cb4154..f45d7e7 100644 --- a/panel.go +++ b/panel.go @@ -16,14 +16,17 @@ import ( "golang.org/x/image/font" ) -var ( - paddingWidth = 60 - paddingHeight = 60 - windowHeight = 20 * 3 - lineWidth = 40 +const ( + paddingWidth = 60 + paddingHeight = 60 + windowHeight = 20 * 3 + windowHeightNoBar = 10 + lineWidth = 40 radius = 10 +) +var ( // default window background color windowBackgroundColor = color.RGBA{40, 42, 54, 255} @@ -141,9 +144,9 @@ func (p *Panel) Draw() error { // window control bar if p.noWindowAccessBar { - windowHeight = 10 + p.drawWindowControlPanel(width, windowHeightNoBar) } else { - p.drawWindowControlPanel(width, height) + p.drawWindowControlPanel(width, windowHeight) } // round corner @@ -159,7 +162,7 @@ func (p *Panel) drawWindowPanel(w, h int) { } func (p *Panel) drawWindowControlPanel(w, h int) { - wc := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, paddingHeight+windowHeight) + wc := NewPanel(paddingWidth, paddingHeight, w-paddingWidth, paddingHeight+h) wc.fillColor(windowBackgroundColor) wc.drawControlButtons() @@ -287,7 +290,14 @@ func (p *Panel) Label(out io.Writer, src io.Reader, filename, language string) e Src: image.NewUniform(color.White), Face: p.fontFace, } - sp := image.Point{X: paddingWidth, Y: paddingHeight + windowHeight} + + spy := paddingHeight + if p.noWindowAccessBar { + spy += windowHeightNoBar + } else { + spy += windowHeight + } + sp := image.Point{X: paddingWidth, Y: spy} p.Formatter = NewPNGFormatter(p.fontSize, drawer, sp, !p.noLineNum) formatters.Register("png", p.Formatter) From e5adab996f08b82205770489b982305845c9cae5 Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sat, 2 Apr 2022 18:31:31 +0900 Subject: [PATCH 3/7] Rewrite test --- cmd/germanium/main.go | 4 +- cmd/germanium/main_test.go | 122 +++++++++++++++++++++++-------------- 2 files changed, 80 insertions(+), 46 deletions(-) diff --git a/cmd/germanium/main.go b/cmd/germanium/main.go index fd520c8..3aa6150 100644 --- a/cmd/germanium/main.go +++ b/cmd/germanium/main.go @@ -7,9 +7,11 @@ import ( "github.com/matsuyoshi30/germanium/cli" ) +var exit = os.Exit + func main() { if err := cli.Run(); err != nil { fmt.Fprintln(os.Stderr, err) - os.Exit(1) + exit(1) } } diff --git a/cmd/germanium/main_test.go b/cmd/germanium/main_test.go index 1872913..efbe2d9 100644 --- a/cmd/germanium/main_test.go +++ b/cmd/germanium/main_test.go @@ -5,63 +5,95 @@ import ( "image" "image/png" "os" - "os/exec" "path/filepath" "reflect" - "strings" "testing" ) var genGoldenFiles = flag.Bool("gen_golden_files", false, "whether to generate the golden files fot test") -func TestRun(t *testing.T) { - filepath.Walk("./testdata", func(path string, info os.FileInfo, err error) error { - if strings.HasSuffix(path, ".sh") { - cmd := exec.Command("bash", filepath.Base(path)) - cmd.Dir = filepath.Dir(path) - if err := cmd.Run(); err != nil { - t.Errorf("FAIL: %v\n", err) - } else { - filename := strings.TrimSuffix(path, filepath.Ext(path)) - if *genGoldenFiles { - if err := os.Rename(filename+"-gen.png", filename+".png"); err != nil { - t.Errorf("FAIL: %v\n", err) - } - t.Logf("Generate file: %s\n", filename+".png") - return nil - } +func TestMain(t *testing.T) { + defaultArg := []string{"germanium", filepath.Join("testdata", "main.go"), "-l", "go"} - wantFile := filename + ".png" - want, err := os.Open(wantFile) - if err != nil { - t.Errorf("FAIL: Reading want file: %s", wantFile) - } - wantImg, err := png.Decode(want) - if err != nil { - t.Errorf("FAIL: Decoding want file: %s", wantFile) - } + tests := []struct { + desc string + args []string + }{ + { + desc: "only-editor", + args: []string{"--no-line-number", "--no-window-access-bar"}, + }, + { + desc: "default", + }, + { + desc: "broken-style", + args: []string{"-s", "pygments"}, + }, + { + desc: "light-style", + args: []string{"-s", "autumn"}, + }, + { + desc: "no-line-num", + args: []string{"--no-line-number"}, + }, + { + desc: "no-window-access-bar", + args: []string{"--no-window-access-bar"}, + }, + { + desc: "style", + args: []string{"-s", "solarized-dark"}, + }, + } - gotFile := filename + "-gen.png" - got, err := os.Open(gotFile) - if err != nil { - t.Errorf("FAIL: Reading got file: %s", gotFile) - } - gotImg, err := png.Decode(got) - if err != nil { - t.Errorf("FAIL: Decoding got file: %s", gotFile) - } + for _, tt := range tests { + tt := tt + t.Run(tt.desc, func(t *testing.T) { + genfile := tt.desc + "-gen.png" + os.Args = append(defaultArg, append(tt.args, "-o", genfile)...) + exit = func(code int) { t.Fatalf("exit %d during main", code) } + + main() - if reflect.DeepEqual(wantImg.(*image.RGBA), gotImg.(*image.RGBA)) { - t.Logf("PASS: %s\n", path) - } else { - t.Errorf("FAIL: output differs: %s\n", path) + if *genGoldenFiles { + if err := os.Rename(genfile, filepath.Join("testdata", tt.desc+".png")); err != nil { + t.Errorf("FAIL: %v\n", err) } + t.Logf("Generate file: %s\n", tt.desc+".png") + return + } + + want, err := os.Open(filepath.Join("testdata", tt.desc+".png")) + if err != nil { + t.Errorf("FAIL: reading want file: %s\n", tt.desc) + } + defer want.Close() + wantImg, err := png.Decode(want) + if err != nil { + t.Errorf("FAIL: decoding want file: %s\n", tt.desc) + } + + got, err := os.Open(genfile) + if err != nil { + t.Errorf("FAIL: reading got file: %s\n", tt.desc) + } + defer got.Close() + gotImg, err := png.Decode(got) + if err != nil { + t.Errorf("FAIL: decoding got file: %s\n", tt.desc) + } + + if !reflect.DeepEqual(wantImg.(*image.RGBA), gotImg.(*image.RGBA)) { + t.Errorf("FAIL: output differs: %s\n", tt.desc) + } - if err := os.Remove(gotFile); err != nil { - t.Errorf("FAIL: cleanup got file: %s\n", path) + if !*genGoldenFiles { + if err := os.Remove(genfile); err != nil { + t.Errorf("FAIL: cleanup got file: %s\n", tt.desc) } } - } - return nil - }) + }) + } } From b19979421b5932065be38aa9b2698ce7e5ff431a Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sat, 2 Apr 2022 18:56:54 +0900 Subject: [PATCH 4/7] Remove unused scripts --- cmd/germanium/testdata/broken-style.sh | 3 --- cmd/germanium/testdata/default.sh | 2 -- cmd/germanium/testdata/light-style.sh | 4 ---- cmd/germanium/testdata/no-line-num.sh | 2 -- cmd/germanium/testdata/no-window-access-bar.sh | 2 -- cmd/germanium/testdata/only-editor.sh | 2 -- cmd/germanium/testdata/style.sh | 2 -- 7 files changed, 17 deletions(-) delete mode 100644 cmd/germanium/testdata/broken-style.sh delete mode 100644 cmd/germanium/testdata/default.sh delete mode 100644 cmd/germanium/testdata/light-style.sh delete mode 100644 cmd/germanium/testdata/no-line-num.sh delete mode 100644 cmd/germanium/testdata/no-window-access-bar.sh delete mode 100644 cmd/germanium/testdata/only-editor.sh delete mode 100644 cmd/germanium/testdata/style.sh diff --git a/cmd/germanium/testdata/broken-style.sh b/cmd/germanium/testdata/broken-style.sh deleted file mode 100644 index 58f21b1..0000000 --- a/cmd/germanium/testdata/broken-style.sh +++ /dev/null @@ -1,3 +0,0 @@ -#!/bin/bash -# Test a style that has no background color or missing colors for some tokens -../../../germanium -s pygments main.go -o broken-style-gen.png diff --git a/cmd/germanium/testdata/default.sh b/cmd/germanium/testdata/default.sh deleted file mode 100644 index 58b414d..0000000 --- a/cmd/germanium/testdata/default.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -../../../germanium main.go -o default-gen.png diff --git a/cmd/germanium/testdata/light-style.sh b/cmd/germanium/testdata/light-style.sh deleted file mode 100644 index a56303c..0000000 --- a/cmd/germanium/testdata/light-style.sh +++ /dev/null @@ -1,4 +0,0 @@ -#!/usr/bin/env bash -# Test a style that has a light background color, and missing colors for some tokens -# The line numbers should be black -../../../germanium -s autumn main.go -o light-style-gen.png diff --git a/cmd/germanium/testdata/no-line-num.sh b/cmd/germanium/testdata/no-line-num.sh deleted file mode 100644 index a693da2..0000000 --- a/cmd/germanium/testdata/no-line-num.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -../../../germanium --no-line-number main.go -o no-line-num-gen.png diff --git a/cmd/germanium/testdata/no-window-access-bar.sh b/cmd/germanium/testdata/no-window-access-bar.sh deleted file mode 100644 index 642e6d9..0000000 --- a/cmd/germanium/testdata/no-window-access-bar.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -../../../germanium --no-window-access-bar main.go -o no-window-access-bar-gen.png diff --git a/cmd/germanium/testdata/only-editor.sh b/cmd/germanium/testdata/only-editor.sh deleted file mode 100644 index 003a84a..0000000 --- a/cmd/germanium/testdata/only-editor.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -../../../germanium --no-line-number --no-window-access-bar main.go -o only-editor-gen.png diff --git a/cmd/germanium/testdata/style.sh b/cmd/germanium/testdata/style.sh deleted file mode 100644 index 845309c..0000000 --- a/cmd/germanium/testdata/style.sh +++ /dev/null @@ -1,2 +0,0 @@ -#!/bin/bash -../../../germanium -s solarized-dark main.go -o style-gen.png From 48f5d5845893426686db8eb628e0f9b1d4f05949 Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sat, 2 Apr 2022 20:56:04 +0900 Subject: [PATCH 5/7] Fix Makefile --- Makefile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Makefile b/Makefile index 9d9dfeb..a53e5ab 100644 --- a/Makefile +++ b/Makefile @@ -7,7 +7,7 @@ BINARY_NAME=germanium all: build test build: $(GOBUILD) -o $(BINARY_NAME) -v ./cmd/$(BINARY_NAME) -test: build +test: $(GOTEST) -v ./... gentest: build $(GOTEST) -v ./... -gen_golden_files From ace6bdfc9c564b24081a75cc1811ffcdca5c75a3 Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sun, 3 Apr 2022 17:36:46 +0900 Subject: [PATCH 6/7] Update staticcheck version --- .github/workflows/test.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/test.yaml b/.github/workflows/test.yaml index 6645c6e..90f7e78 100644 --- a/.github/workflows/test.yaml +++ b/.github/workflows/test.yaml @@ -13,7 +13,7 @@ jobs: test: strategy: matrix: - go-version: [1.16.x] + go-version: [1.17.x, 1.18.x] os: [ubuntu-latest, macos-latest] runs-on: ${{ matrix.os }} steps: @@ -24,8 +24,8 @@ jobs: - name: Checkout code uses: actions/checkout@v2 - name: Build - run: go build - - uses: dominikh/staticcheck-action@v1.0.0 + run: make build + - uses: dominikh/staticcheck-action@v1.2.0 name: staticcheck with: install-go: false From 34b07845c33a33a5da0b6b5ec6c7e5d996ee1289 Mon Sep 17 00:00:00 2001 From: matsuyoshi30 Date: Sun, 3 Apr 2022 18:00:47 +0900 Subject: [PATCH 7/7] Update README --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 613e0f3..fac107d 100644 --- a/README.md +++ b/README.md @@ -88,7 +88,7 @@ cd cmd/germanium && go install #### Requirements -- go1.16 +- Go (1.17 or 1.18) ## Related Projects