Skip to content

Commit

Permalink
Merge pull request #21 from tbhaxor/feat/unittesting
Browse files Browse the repository at this point in the history
Implement unit testing and fix bug in add.go
  • Loading branch information
Aarush Goyal authored Jul 14, 2022
2 parents e5ac919 + 646d591 commit aef338c
Show file tree
Hide file tree
Showing 7 changed files with 443 additions and 35 deletions.
47 changes: 32 additions & 15 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
name: Go

on:
push:
branches: [main]
pull_request:
branches: [main]
branches:
- main
types:
- opened
- synchronize
push:
branches:
- main

jobs:
build:
runs-on: ubuntu-latest
testing:
name: "Unit Testing"
runs-on: "${{ matrix.os }}"
steps:
- uses: actions/checkout@v2

- name: Set up Go
- name: "Clone repository"
uses: actions/checkout@v2
- name: "Set up Go"
uses: actions/setup-go@v2
with:
go-version: 1.17

- name: Build
run: go build -v ./...

- name: Test
run: go test -v ./...
go-version: "${{ matrix.golang }}"
- name: "Perform Unit Test"
run: "go test -v ./..."
- name: "Build and Install"
run: |
go build -v ./...
go install -v ./...
strategy:
matrix:
golang:
- 1.15
- 1.16
- 1.17
- 1.18
os:
- ubuntu-latest
- macos-latest
- windows-latest
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,7 @@
# Go workspace file
go.work

husky
husky

# Goland
.idea
11 changes: 6 additions & 5 deletions internal/lib/add.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"os"
"path"
)

func Add(hook string, cmd string) error {
Expand All @@ -15,7 +16,7 @@ func Add(hook string, cmd string) error {
// check if .git exists
if isExists, err := gitExists(); err == nil && !isExists {
return errors.New("git not initialized")
} else if err == nil {
} else if err != nil {
return err
}

Expand All @@ -27,13 +28,13 @@ func Add(hook string, cmd string) error {
}

// check if .husky/hooks exists
_, err := os.Stat(".husky/hooks")

_, err := os.Stat(getHuskyHooksDir(true))
fmt.Println(err)
if os.IsNotExist(err) {
fmt.Println("no pre-existing hooks found")

// create .husky/hooks
err = os.Mkdir(".husky/hooks", 0755)
err = os.MkdirAll(getHuskyHooksDir(true), 0755)
if err != nil {
return err
}
Expand All @@ -42,7 +43,7 @@ func Add(hook string, cmd string) error {
}

// create hook
file, err := os.Create(".husky/hooks/" + hook)
file, err := os.Create(path.Join(getHuskyHooksDir(true), hook))
if err != nil {
return err
}
Expand Down
12 changes: 4 additions & 8 deletions internal/lib/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package lib
import (
"errors"
"os"
"path"
)

func Init() error {
Expand All @@ -20,19 +21,14 @@ func Init() error {
return err
}

// if not, create .husky
err := os.Mkdir(".husky", 0755)
if err != nil {
return err
}

err = os.Mkdir(".husky/hooks", 0755)
// if not, create .husky/hooks
err := os.MkdirAll(getHuskyHooksDir(true), 0755)
if err != nil {
return err
}

// create default pre-commit hook
file, err := os.Create(".husky/hooks/pre-commit")
file, err := os.Create(path.Join(getHuskyHooksDir(true), "pre-commit"))
if err != nil {
return err
}
Expand Down
3 changes: 2 additions & 1 deletion internal/lib/install.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@ func Install() error {
return err
}

gitHooksDir, huskyHooksDir := getGitHooksDir(true), getHuskyHooksDir(true)
// check if .husky/hooks exists
_, err := os.Stat(".husky/hooks")
_, err := os.Stat(huskyHooksDir)
if os.IsNotExist(err) {
return errors.New("no hooks found")
}
Expand Down
Loading

0 comments on commit aef338c

Please sign in to comment.