Skip to content

Commit

Permalink
CI test add windows environment run and close open file (#9)
Browse files Browse the repository at this point in the history
* add windows and mac os to test ci

* explicity cleanup tmpDir

* explicity cleanup tmpDir

* test cleanup

* modify test

* modify test

* test

* modify test

* close open file

* install staticcheck, use cache

* use bash CI test task

* cache all go modules

* test
  • Loading branch information
JY8752 committed Sep 17, 2023
1 parent 59f0294 commit 004760d
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 12 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,35 @@ on: [push]

jobs:
test-and-lint:
runs-on: ubuntu-latest
strategy:
matrix:
os: [ubuntu-latest, macos-latest, windows-latest]
runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v4
# goのセットアップ
- name: Set up golang
uses: actions/setup-go@v4
with:
go-version: '>=1.20'
# Go module cache
- name: Cache Go modules
uses: actions/cache@v2
with:
path: |
~/go/pkg/mod
~/go/bin/*
key: ${{ runner.os }}-go-${{ hashFiles('**/go.sum') }}
restore-keys: |
${{ runner.os }}-go-
# staticcheckインストール
- name: Install staticcheck
run: go install honnef.co/go/tools/cmd/staticcheck@latest
run: |
if ! command -v staticcheck &> /dev/null
then
go install honnef.co/go/tools/cmd/staticcheck@latest
fi
shell: bash # Windows環境考慮
# build
- name: Build
run: go build
Expand Down
21 changes: 15 additions & 6 deletions internal/file/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package file_test

import (
"os"
"path/filepath"
"testing"

"github.com/JY8752/note-cli/internal/file"
Expand All @@ -10,36 +11,44 @@ import (
func TestExist(t *testing.T) {
tmpdir := t.TempDir()

if err := os.Mkdir(tmpdir+"/dir", 0777); err != nil {
if err := os.Mkdir(filepath.Join(tmpdir, "dir"), 0777); err != nil {
t.Fatal(err)
}
if _, err := os.OpenFile(tmpdir+"/dir/test.txt", os.O_CREATE, 0777); err != nil {

f, err := os.OpenFile(filepath.Join(tmpdir, "dir", "test.txt"), os.O_CREATE, 0777)
if err != nil {
t.Fatal(err)
}

t.Cleanup(func() {
if err := f.Close(); err != nil {
t.Fatal(err)
}
})

testcases := []struct {
name string
path string
want bool
}{
{
"file is exist",
tmpdir + "/dir/test.txt",
filepath.Join(tmpdir, "dir", "test.txt"),
true,
},
{
"file is not exist",
tmpdir + "/dir/test2.txt",
filepath.Join(tmpdir, "dir", "test2.txt"),
false,
},
{
"directory is exist",
tmpdir + "/dir",
filepath.Join(tmpdir, "dir"),
true,
},
{
"directory is not exist",
tmpdir + "/dir2",
filepath.Join(tmpdir, "dir2"),
false,
},
}
Expand Down
14 changes: 10 additions & 4 deletions internal/run/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ type Options struct {
type Option func(*Options)

func CreateArticleFunc(timeFlag *bool, name *string, options ...Option) RunEFunc {
return func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
t := *timeFlag
n := *name

Expand Down Expand Up @@ -73,23 +73,29 @@ func CreateArticleFunc(timeFlag *bool, name *string, options ...Option) RunEFunc

// mkdir
targetDir := filepath.Join(op.BasePath, dirName)
if err := os.Mkdir(targetDir, 0744); err != nil {
if err = os.Mkdir(targetDir, 0744); err != nil {
return err
}

fmt.Printf("Create directory. %s\n", targetDir)

// create markdown file
filePath := filepath.Join(targetDir, fmt.Sprintf("%s.md", dirName))
if _, err := os.OpenFile(filePath, os.O_CREATE, 0644); err != nil {
f, err := os.OpenFile(filePath, os.O_CREATE, 0644)
defer func() {
if err = f.Close(); err != nil {
fmt.Printf("failed close file. file: %s\n", filePath)
}
}()
if err != nil {
return err
}

fmt.Printf("Create file. %s\n", filePath)

// create config.yaml
configFilePath := filepath.Join(targetDir, ConfigFile)
if err := os.WriteFile(configFilePath, []byte("title: article title\nauthor: your name"), 0644); err != nil {
if err = os.WriteFile(configFilePath, []byte("title: article title\nauthor: your name"), 0644); err != nil {
return err
}

Expand Down

0 comments on commit 004760d

Please sign in to comment.