Skip to content

Commit

Permalink
Merge pull request #10 from matsuyoshi30/add-tests
Browse files Browse the repository at this point in the history
Add tests
  • Loading branch information
matsuyoshi30 authored Apr 5, 2021
2 parents b21d9b2 + 967bc43 commit b466d9b
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 2 deletions.
32 changes: 32 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
name: test
on:
push:
branches:
- master
paths-ignore:
- 'README.md'
- '.github/'
pull_request:
branches:
- master
paths-ignore:
- 'README.md'
- '.github/'
jobs:
test:
strategy:
matrix:
go-version: [1.16.x]
os: [ubuntu-latest, macos-latest]
runs-on: ${{ matrix.os }}
steps:
- name: Install Go
uses: actions/setup-go@v2
with:
go-version: ${{ matrix.go-version }}
- name: Checkout code
uses: actions/checkout@v2
- name: Build
run: go build
- name: Test
run: make test
6 changes: 4 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ GOCLEAN=$(GOCMD) clean
GOTEST=$(GOCMD) test
BINARY_NAME=germanium

all: test build
all: build test
build:
$(GOBUILD) -o $(BINARY_NAME) -v
test:
test: build
$(GOTEST) -v ./...
gentest:
$(GOTEST) -gen_golden_files ./...
clean:
$(GOCLEAN)
rm -f $(BINARY_NAME)
Expand Down
67 changes: 67 additions & 0 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package main

import (
"flag"
"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+"-want.png")
return nil
}

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)
}

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)
}

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 err := os.Remove(gotFile); err != nil {
t.Errorf("FAIL: cleanup got file: %s\n", path)
}
}
}
return nil
})
}
Binary file added testdata/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions testdata/default.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../germanium main.go -o default-gen.png
Binary file added testdata/no-line-num.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions testdata/no-line-num.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../germanium --no-line-number main.go -o no-line-num-gen.png
Binary file added testdata/no-window-access-bar.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions testdata/no-window-access-bar.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../germanium --no-window-access-bar main.go -o no-window-access-bar-gen.png
Binary file added testdata/only-editor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions testdata/only-editor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
../germanium --no-line-number --no-window-access-bar main.go -o only-editor-gen.png

0 comments on commit b466d9b

Please sign in to comment.