Skip to content

Commit

Permalink
Increases code coverage and updates devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
djschleen authored Mar 1, 2024
1 parent fb4a7ac commit 9da6537
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 88 deletions.
11 changes: 8 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"ghcr.io/devcontainers-contrib/features/starship:1": {},
"ghcr.io/azutake/devcontainer-features/go-packages-install:0": {
"packages": [
"github.com/devops-kung-fu/hookz@latest",
"github.com/jandelgado/gcov2lcov@latest",
"github.com/kisielk/errcheck@latest",
"github.com/fzipp/gocyclo/cmd/gocyclo@latest",
Expand All @@ -16,7 +17,8 @@
"customizations": {
"vscode": {
"settings": {
"terminal.integrated.fontFamily": "DroidSansMNerdFont-Regular",
"terminal.integrated.customGlyphs": true,
"terminal.integrated.fontFamily": "'0xProto Nerd Font', 'Droid Sans Mono', 'monospace', monospace",
"editor.formatOnSave": true,
"go.buildTags": "",
"go.toolsEnvVars": {
Expand All @@ -40,7 +42,10 @@
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
}
},
"markiscodecoverage.coverageThreshold": 95,
"markiscodecoverage.enableOnStartup": true,
"markiscodecoverage.searchCriteria": "*.lcov*"
},
"extensions": [
"ms-vscode.go",
Expand All @@ -61,5 +66,5 @@
]
}
},
"postCreateCommand": "go mod download && go mod tidy && mkdir -p ~/.local/share/fonts && cd ~/.local/share/fonts && curl -fLO https://github.com/ryanoasis/nerd-fonts/raw/HEAD/patched-fonts/DroidSansMono/DroidSansMNerdFont-Regular.otf"
"postCreateCommand": "/usr/bin/bash ./.devcontainer/post-create.sh > ~/post-create.log"
}
6 changes: 6 additions & 0 deletions .devcontainer/post-create.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
mkdir -p $HOME/.local/share/fonts
wget https://github.com/ryanoasis/nerd-fonts/releases/download/v3.1.1/0xProto.zip
unzip 0xProto.zip -d $HOME/.local/share/fonts
rm 0xProto.zip

starship preset nerd-font-symbols -o ~/.config/starship.toml
20 changes: 7 additions & 13 deletions cmd/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,36 +4,30 @@ import (
"errors"
"fmt"
"log"
"os"

"github.com/devops-kung-fu/common/util"

"github.com/devops-kung-fu/hookz/lib"
)

func noConfig() {
func NoConfig() {

Check failure on line 13 in cmd/common.go

View workflow job for this annotation

GitHub Actions / tests

exported function NoConfig should have comment or be unexported
fmt.Println(".hookz.yaml file not found")
fmt.Println("\nTo create a sample configuration run:")
fmt.Println(" hookz init config")
fmt.Println("\nRun 'hookz --help' for usage.")
fmt.Println()
os.Exit(1)
}

func badYaml() {
util.PrintErr(errors.New("configuration in .hookz.yaml is not valid YAML syntax"))
os.Exit(1)
}

// CheckConfig ensures that there is a .hookz.yaml file locally and the version is supported by the current version of hookz
func CheckConfig() (config lib.Configuration) {
config, err := lib.ReadConfig(Afs, version)
func CheckConfig() (config lib.Configuration, err error) {
config, err = lib.ReadConfig(Afs, version)
var returnErr error

Check warning on line 24 in cmd/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/common.go#L22-L24

Added lines #L22 - L24 were not covered by tests
if err != nil && err.Error() == "NO_CONFIG" {
noConfig()
returnErr = errors.New("NO_CONFIG")

Check warning on line 26 in cmd/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/common.go#L26

Added line #L26 was not covered by tests
} else if err != nil && err.Error() == "BAD_YAML" {
badYaml()
returnErr = errors.New("configuration in .hookz.yaml is not valid YAML syntax")

Check warning on line 28 in cmd/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/common.go#L28

Added line #L28 was not covered by tests
}
return
return config, returnErr

Check warning on line 30 in cmd/common.go

View check run for this annotation

Codecov / codecov/patch

cmd/common.go#L30

Added line #L30 was not covered by tests
}

// InstallSources installs all go repositories that are found in the Sources section of the .hookz.yaml file.
Expand Down
41 changes: 41 additions & 0 deletions cmd/common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package cmd

import (
"testing"

"github.com/devops-kung-fu/common/util"
"github.com/stretchr/testify/assert"

"github.com/devops-kung-fu/hookz/lib"
)

func Test_InstallSources(t *testing.T) {
sources := []lib.Source{
{
Source: "github.com/devops-kung-fu/hinge@latest",
},
}
output := util.CaptureOutput(func() {
_ = InstallSources(sources)
})

assert.NotNil(t, output)
assert.Contains(t, output, "go install github.com/devops-kung-fu/hinge@latest\n")

sources = []lib.Source{
{
Source: "yeah",
},
}
output = util.CaptureOutput(func() {
_ = InstallSources(sources)
})
assert.Contains(t, output, "exit status 1\n")
}

func TestNoConfig(t *testing.T) {
output := util.CaptureOutput(func() {
NoConfig()
})
assert.NotNil(t, output)
}
10 changes: 9 additions & 1 deletion cmd/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ var (
util.DoIf(Verbose, func() {
util.PrintInfo("Creating hooks")
})
config := CheckConfig()
config, err := CheckConfig()
if err != nil {
if err != nil && err.Error() == "NO_CONFIG" {
NoConfig()
} else {
util.PrintErr(err)

Check warning on line 39 in cmd/init.go

View check run for this annotation

Codecov / codecov/patch

cmd/init.go#L34-L39

Added lines #L34 - L39 were not covered by tests
}
os.Exit(1)

Check warning on line 41 in cmd/init.go

View check run for this annotation

Codecov / codecov/patch

cmd/init.go#L41

Added line #L41 was not covered by tests
}
_ = InstallSources(config.Sources)
if util.IsErrorBool(lib.WriteHooks(Afs, config, Verbose, VerboseOutput)) {
return
Expand Down
12 changes: 11 additions & 1 deletion cmd/reset.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/devops-kung-fu/common/util"
"github.com/spf13/cobra"

Expand All @@ -19,7 +21,15 @@ var (
if util.IsErrorBool(lib.RemoveHooks(Afs, Verbose)) {
return
}
config := CheckConfig()
config, err := CheckConfig()
if err != nil {
if err != nil && err.Error() == "NO_CONFIG" {
NoConfig()
} else {
util.PrintErr(err)

Check warning on line 29 in cmd/reset.go

View check run for this annotation

Codecov / codecov/patch

cmd/reset.go#L24-L29

Added lines #L24 - L29 were not covered by tests
}
os.Exit(1)

Check warning on line 31 in cmd/reset.go

View check run for this annotation

Codecov / codecov/patch

cmd/reset.go#L31

Added line #L31 was not covered by tests
}
_ = InstallSources(config.Sources)
if util.IsErrorBool(lib.WriteHooks(Afs, config, Verbose, VerboseOutput)) {
return
Expand Down
22 changes: 0 additions & 22 deletions cmd/reset_test.go

This file was deleted.

21 changes: 0 additions & 21 deletions cmd/root_test.go

This file was deleted.

12 changes: 11 additions & 1 deletion cmd/update.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package cmd

import (
"os"

"github.com/devops-kung-fu/common/util"
"github.com/spf13/cobra"

Expand All @@ -16,7 +18,15 @@ var (
util.DoIf(Verbose, func() {
util.PrintInfo("Updating sources and executables")
})
config := CheckConfig()
config, err := CheckConfig()
if err != nil {
if err != nil && err.Error() == "NO_CONFIG" {
NoConfig()
} else {
util.PrintErr(err)

Check warning on line 26 in cmd/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/update.go#L21-L26

Added lines #L21 - L26 were not covered by tests
}
os.Exit(1)

Check warning on line 28 in cmd/update.go

View check run for this annotation

Codecov / codecov/patch

cmd/update.go#L28

Added line #L28 was not covered by tests
}
_ = InstallSources(config.Sources)
updateCount, _ := lib.UpdateExecutables(Afs, config)
util.DoIf(updateCount == 0, func() {
Expand Down
22 changes: 0 additions & 22 deletions cmd/update_test.go

This file was deleted.

5 changes: 1 addition & 4 deletions lib/hookwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,7 @@ func buildExec(afs *afero.Afero, action *Action) (err error) {
}
if action.Exec == nil && action.Script != nil {
scriptFileName, _ := CreateScriptFile(afs, *action.Script)
path, err := os.Getwd()
if err != nil {
return err
}
path, _ := os.Getwd()
fullScriptFileName := fmt.Sprintf("%s/%s/%s", path, ".git/hooks", scriptFileName)
action.Exec = &fullScriptFileName
}
Expand Down

0 comments on commit 9da6537

Please sign in to comment.