Skip to content

Commit

Permalink
Fixes error checking, updated devcontainer
Browse files Browse the repository at this point in the history
  • Loading branch information
djschleen authored Feb 29, 2024
1 parent 962a5de commit 648d1e9
Show file tree
Hide file tree
Showing 14 changed files with 157 additions and 59 deletions.
69 changes: 66 additions & 3 deletions .devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
{
"name": "hookz-devcontainer",
"name": "devcontainer-test",
"image": "mcr.microsoft.com/devcontainers/go:1-1.22-bookworm",
"features": {
"ghcr.io/devcontainers-contrib/features/starship:1": {}
}
"ghcr.io/devcontainers-contrib/features/starship:1": {},
"ghcr.io/azutake/devcontainer-features/go-packages-install:0": {
"packages": [
"github.com/jandelgado/gcov2lcov@latest",
"github.com/kisielk/errcheck@latest",
"github.com/fzipp/gocyclo/cmd/gocyclo@latest",
"golang.org/x/vuln/cmd/govulncheck@latest",
"honnef.co/go/tools/cmd/staticcheck@latest"
]
}
},
"customizations": {
"vscode": {
"settings": {
"files.eol": "\n",
"terminal.integrated.fontFamily": "DroidSansMNerdFont-Regular",
"editor.formatOnSave": true,
"go.buildTags": "",
"go.toolsEnvVars": {
"CGO_ENABLED": "0"
},
"go.useLanguageServer": true,
"go.testEnvVars": {
"CGO_ENABLED": "1"
},
"go.testFlags": [
"-v",
"-race"
],
"go.testTimeout": "10s",
"go.coverOnSingleTest": true,
"go.coverOnSingleTestFile": true,
"go.coverOnTestPackage": true,
"go.lintTool": "golangci-lint",
"go.lintOnSave": "package",
"[go]": {
"editor.codeActionsOnSave": {
"source.organizeImports": "always"
}
},
"gopls": {
"usePlaceholders": false,
"staticcheck": true,
"vulncheck": "Imports"
},
},
"extensions": [
"ms-vscode.go",
"golang.go",
"github.vscode-pull-request-github",
"github.vscode-github-actions",
"github.vscode-codeql",
"aleksandra.go-group-imports",
"oderwat.indent-rainbow",
"yzhang.markdown-all-in-one",
"quicktype.quicktype",
"jebbs.plantuml",
"foxundermoon.shell-format",
"ahebrank.yaml2json",
"defaltd.go-coverage-viewer",
"Gruntfuggly.todo-tree", // Highlights TODO comments"
]
}
},
"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"
}
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,6 @@
hookz
coverage.out
coverage.html
coverage.lcov

.DS_Store
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ title:

build: ## Builds the application
go get -u ./...
@go mod tidy
@go build
go mod tidy
go build

check: build ## Tests the pre-commit hooks if they exist
./hookz reset --verbose --debug --verbose-output
. .git/hooks/pre-commit

test: ## Runs tests and coverage
@go test -v -coverprofile=coverage.out ./... && go tool cover -func=coverage.out
@go tool cover -html=coverage.out -o coverage.html
go test -v -coverprofile=coverage.out ./... && go tool cover -func=coverage.out && gcov2lcov -infile=coverage.out -outfile=coverage.lcov
go tool cover -html=coverage.out -o coverage.html

install: build ## Builds an executable local version of Hookz and puts in in /usr/local/bin
@sudo chmod +x hookz
Expand Down
22 changes: 22 additions & 0 deletions cmd/reset_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// cmd/root_test.go
package cmd

import (
"testing"

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

func TestExecuteCommand(t *testing.T) {
// Set up a test command
cmd := resetCmd

// Capture standard output for testing
output := util.CaptureOutput(func() {
cmd.SetArgs([]string{"reset"})
_ = cmd.Execute()
})

assert.Contains(t, output, "Manages commit hooks inside a local git repository")
}
21 changes: 21 additions & 0 deletions cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// cmd/root_test.go
package cmd

import (
"testing"

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

func Test_rootCommand(t *testing.T) {
// Set up a test command
cmd := updateCmd

// Capture standard output for testing
output := util.CaptureOutput(func() {
_ = cmd.Execute()
})

assert.Contains(t, output, "Manages commit hooks inside a local git repository")
}
22 changes: 22 additions & 0 deletions cmd/update_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// cmd/root_test.go
package cmd

import (
"testing"

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

func Test_updateCommand(t *testing.T) {
// Set up a test command
cmd := updateCmd

// Capture standard output for testing
output := util.CaptureOutput(func() {
cmd.SetArgs([]string{"update"})
_ = cmd.Execute()
})

assert.Contains(t, output, "Manages commit hooks inside a local git repository")
}
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/devops-kung-fu/common v0.2.6 h1:HNL9suXELXHiSg7Ze0VinNkbngrBjovKYWPOckuarKc=
github.com/devops-kung-fu/common v0.2.6/go.mod h1:ZLp6W5ewDWxmx45KF/Oj3IfJ3EhRALBkcfqLQnz23OU=
github.com/devops-kung-fu/common v0.2.6 h1:HNL9suXELXHiSg7Ze0VinNkbngrBjovKYWPOckuarKc=
github.com/devops-kung-fu/common v0.2.6/go.mod h1:ZLp6W5ewDWxmx45KF/Oj3IfJ3EhRALBkcfqLQnz23OU=
github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY=
github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto=
github.com/gookit/color v1.5.4 h1:FZmqs7XOyGgCAxmWyPslpiok1k05wmY3SJTytgvYFs0=
Expand Down
4 changes: 2 additions & 2 deletions lib/configreader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func Test_ReadConfig(t *testing.T) {
assert.Error(t, err, "There should be no config created so an error should be thrown.")
assert.Equal(t, "NO_CONFIG", err.Error())

CreateConfig(afs, "1.0.0")
_, _ = CreateConfig(afs, "1.0.0")
readConfig, err := ReadConfig(afs, version)

assert.NoError(t, err, "ReadConfig should not have generated an error")
Expand All @@ -37,7 +37,7 @@ func Test_ReadConfig(t *testing.T) {
func Test_badConfig(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}
filename, _ := filepath.Abs(".hookz.yaml")
afs.WriteFile(filename, badConfig(), 0644)
_ = afs.WriteFile(filename, badConfig(), 0644)

_, err := ReadConfig(afs, version)
assert.Error(t, err)
Expand Down
12 changes: 3 additions & 9 deletions lib/configvalidator.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,16 @@ import (
"github.com/spf13/afero"
)

func generateShasum(afs *afero.Afero) (shasum string, err error) {
func generateShasum(afs *afero.Afero) (shasum string) {
filename, _ := filepath.Abs(".hookz.yaml")
yamlFile, err := afs.ReadFile(filename)
if err != nil {
return
}
yamlFile, _ := afs.ReadFile(filename)
shasum = fmt.Sprintf("%x", sha256.Sum256(yamlFile))
return
}

// WriteShasum writes the shasum of the JSON representation of the configuration to hookz.shasum
func WriteShasum(afs *afero.Afero) (err error) {
shasum, err := generateShasum(afs)
if err != nil {
return err
}
shasum := generateShasum(afs)
filename, _ := filepath.Abs(".git/hooks/hookz.shasum")
err = afs.WriteFile(filename, []byte(shasum), 0644)
util.IfErrorLog(err)
Expand Down
9 changes: 4 additions & 5 deletions lib/configvalidator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,16 @@ import (
func Test_generateShasum(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}

CreateConfig(afs, version)
_, _ = CreateConfig(afs, version)

shasum, err := generateShasum(afs)
assert.NoError(t, err, "Likely .hookz.yaml couldn't be read")
shasum := generateShasum(afs)
assert.Equal(t, "d6e393b32ffa1a804b705d0a60acedd9c983a6d2e01cd1871a2e75ec358a5c20", shasum, "shasums do not match, but should")
}

func Test_WriteShasum(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}

CreateConfig(afs, version)
_, _ = CreateConfig(afs, version)

err := WriteShasum(afs)
assert.NoError(t, err, "A shasum write should not have caused an error")
Expand All @@ -38,7 +37,7 @@ func Test_WriteShasum(t *testing.T) {
func TestDeps_CheckVersion(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}

CreateConfig(afs, version)
_, _ = CreateConfig(afs, version)

readConfig, err := ReadConfig(afs, version)
assert.NoError(t, err, "ReadConfig should not have generated an error")
Expand Down
6 changes: 1 addition & 5 deletions lib/configwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,7 @@ func CreateConfig(afs *afero.Afero, version string) (config Configuration, err e
},
}

file, memoryErr := yaml.Marshal(config)
if memoryErr != nil {
err = memoryErr
return
}
file, _ := yaml.Marshal(config)
filename, _ := filepath.Abs(".hookz.yaml")
err = afs.WriteFile(filename, file, 0644)

Expand Down
5 changes: 1 addition & 4 deletions lib/hookdeleter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ import (
// RemoveHooks removes hooks with a specific extension and their corresponding files in the Git hooks directory.
// It also optionally prints information about deleted hooks if verbose is set to true.
func RemoveHooks(afs *afero.Afero, verbose bool) error {
path, err := os.Getwd()
if err != nil {
return err
}
path, _ := os.Getwd()

ext := ".hookz"
hooksPath := filepath.Join(path, ".git/hooks")
Expand Down
4 changes: 2 additions & 2 deletions lib/hookdeleter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestFileSystem_RemoveHooks(t *testing.T) {
path, _ := os.Getwd()

content := "Test Script"
CreateScriptFile(afs, content)
_, _ = CreateScriptFile(afs, content)

p := fmt.Sprintf("%s/%s", path, ".git/hooks")
dirFiles, _ := afs.ReadDir(p)
Expand All @@ -32,7 +32,7 @@ func TestFileSystem_RemoveHooks(t *testing.T) {
func Test_removeShasum(t *testing.T) {
afs := &afero.Afero{Fs: afero.NewMemMapFs()}

CreateConfig(afs, version)
_, _ = CreateConfig(afs, version)
_ = WriteShasum(afs)

path, _ := os.Getwd()
Expand Down
31 changes: 8 additions & 23 deletions lib/hookwriter.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,47 +24,32 @@ type command struct {
// CreateFile creates a file for a provided FileSystem and file name
func CreateFile(afs *afero.Afero, name string) (err error) {

file, err := afs.Fs.Create(name)
_, err = afs.Create(name)
if err != nil {
return err
}

defer func() {
err = file.Close()
}()
// defer func() {
// err = file.Close()
// }()

return
}

// CreateScriptFile creates an executable script file with a random name given a string of content
func CreateScriptFile(afs *afero.Afero, content string) (name string, err error) {

k, idErr := ksuid.NewRandom()
k, _ := ksuid.NewRandom()
name = k.String()
if util.IsErrorBool(idErr) {
err = idErr
return
}
path, _ := os.Getwd()
p := fmt.Sprintf("%s/%s", path, ".git/hooks")

hookzFile := fmt.Sprintf("%s/%s.hookz", p, name)
scriptName := fmt.Sprintf("%s/%s", p, name)

err = CreateFile(afs, hookzFile)
if err != nil {
return
}

err = afs.WriteFile(scriptName, []byte(content), 0644)
if err != nil {
return
}
_, _ = afs.Create(hookzFile)
_ = afs.WriteFile(scriptName, []byte(content), 0644)

err = afs.Fs.Chmod(scriptName, 0777)
if err != nil {
return
}
_ = afs.Fs.Chmod(scriptName, 0777)

return
}
Expand Down

0 comments on commit 648d1e9

Please sign in to comment.