Skip to content

Commit

Permalink
add: test and getting coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
d-tsuji committed Apr 24, 2020
1 parent 5ac6d02 commit 8b5b578
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 29 deletions.
64 changes: 40 additions & 24 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
name: test
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macOS-latest
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.x
- name: Add $GOPATH/bin to $PATH
run: echo "::add-path::$(go env GOPATH)/bin"
- name: Test
run: make test
- name: Lint
run: make lint
name: test
on: [push, pull_request]
jobs:
test:
runs-on: ${{ matrix.os }}
strategy:
fail-fast: false
matrix:
os:
- ubuntu-latest
- macOS-latest
steps:
- name: Checkout code
uses: actions/checkout@master
- name: Setup Go
uses: actions/setup-go@v1
with:
go-version: 1.x
- name: Add $GOPATH/bin to $PATH
run: echo "::add-path::$(go env GOPATH)/bin"
- name: Test
run: make test-cover
- name: Lint
run: make lint
- name: Send coverage
uses: shogo82148/actions-goveralls@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
path-to-profile: c.out
parallel: true
job-number: ${{ strategy.job-index }}
finish:
runs-on: ubuntu-latest
needs: test
steps:
- name: finish coverage report
uses: shogo82148/actions-goveralls@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
parallel-finished: true
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ test: deps
go test -v -count=1 ./...

test-cover: deps
go test -v -cover -coverprofile=c.out
go test -v -count=1 ./... -cover -coverprofile=c.out
go tool cover -html=c.out -o coverage.html

lint: devel-deps
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Qiisync

[![GitHub release](https://img.shields.io/github/v/release/d-tsuji/qiisync.svg)](https://github.com/d-tsuji/qiisync/releases/latest) [![Go Report Card](https://goreportcard.com/badge/github.com/d-tsuji/qiisync)](https://goreportcard.com/report/github.com/d-tsuji/qiisync) [![Actions Status](https://github.com/d-tsuji/qiisync/workflows/test/badge.svg)](https://github.com/d-tsuji/qiisync/actions)
[![GitHub release](https://img.shields.io/github/v/release/d-tsuji/qiisync.svg)](https://github.com/d-tsuji/qiisync/releases/latest) [![Go Report Card](https://goreportcard.com/badge/github.com/d-tsuji/qiisync)](https://goreportcard.com/report/github.com/d-tsuji/qiisync) [![Actions Status](https://github.com/d-tsuji/qiisync/workflows/test/badge.svg)](https://github.com/d-tsuji/qiisync/actions) [![Coverage Status](https://coveralls.io/repos/github/d-tsuji/qiisync/badge.svg?branch=master)](https://coveralls.io/github/d-tsuji/qiisync?branch=master)

<img src="img/logo.png" width="300">

Expand Down
56 changes: 53 additions & 3 deletions config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,61 @@ package qiisync

import (
"io"
"reflect"
"io/ioutil"
"os"
"path/filepath"
"strings"
"testing"

"github.com/google/go-cmp/cmp"
)

func TestLoadConfiguration(t *testing.T) {
tempDir, err := ioutil.TempDir("testdata", "temp")
t.Cleanup(func() {
if err := os.RemoveAll(tempDir); err != nil {
t.Errorf("remove tempDir: %v", err)
}
})
if err != nil {
t.Errorf("create tempDir: %v", err)
}

os.Setenv("HOME", tempDir)
os.Setenv("home", tempDir)
os.Setenv("USERPROFILE", tempDir)

if err := os.MkdirAll(filepath.Join(tempDir, ".config", "qiisync"), 0755); err != nil {
t.Errorf("create config dir: %v", err)
}

f, err := os.Create(filepath.Join(tempDir, ".config", "qiisync", "config"))
if err != nil {
t.Errorf("create config: %v", err)
}
defer f.Close()

f.WriteString(`[qiita]
api_token = "1234567890abcdefghijklmnopqrstuvwxyz1234"
[local]
base_dir = "./testdata/qiita"
filename_mode = "title"
`)

got, err := LoadConfiguration()
if err != nil {
t.Errorf("loca config: %v", err)
}
want := &Config{
Qiita: qiitaConfig{Token: "1234567890abcdefghijklmnopqrstuvwxyz1234"},
Local: localConfig{Dir: "./testdata/qiita", FileNameMode: "title"},
}
if diff := cmp.Diff(want, got); diff != "" {
t.Errorf("LoadConfiguration() mismatch (-want +got):\n%s", diff)
}
}

func Test_loadConfig(t *testing.T) {
type args struct {
r io.Reader
Expand Down Expand Up @@ -73,8 +123,8 @@ filename_mode = "title"`),
t.Errorf("loadConfig() error = %v, wantErr %v", err, tt.wantErr)
return
}
if !reflect.DeepEqual(got, tt.want) {
t.Errorf("loadConfig() got = %v, want %v", got, tt.want)
if diff := cmp.Diff(tt.want, got); diff != "" {
t.Errorf("loadConfig() mismatch (-want +got):\n%s", diff)
}
})
}
Expand Down

0 comments on commit 8b5b578

Please sign in to comment.