Skip to content

Commit

Permalink
refactor: switch from Ginkgo to stdlib testing (#57)
Browse files Browse the repository at this point in the history
* refactor: switch add_test to testing pkg

* refactor: switch multiple test files to testing pkg

* refactor: all of cmd to use testing library instead of ginkgo

* refactor: move most of pkg/kubeconfig to testing pkg

* refactor: convert most of kubeconfig to testing pkg

* refactor: switch all of kubeconfig to testing pkg

* refactor: cleanup remaining ginkgo items

* chore: add tool-versions file for asdf

* docs: update pull request template
  • Loading branch information
particledecay authored Nov 14, 2023
1 parent e1c0df2 commit 4621b5f
Show file tree
Hide file tree
Showing 28 changed files with 1,793 additions and 1,429 deletions.
2 changes: 1 addition & 1 deletion .github/pull_request_template.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,6 @@ Check your PR for the following:

- [ ] you included tests
- [ ] you linted your code
- [ ] your PR has only one commit (interactive rebase!)
- [ ] your PR has appropriate, atomic commits (interactive rebase!)
- [ ] your commit message follows Conventional Commit format
- [ ] you are not reducing the total test coverage
5 changes: 1 addition & 4 deletions .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,9 @@ jobs:
- name: Install dependencies
run: |
go mod vendor
- name: Install Ginkgo
run: |
go install github.com/onsi/ginkgo/v2/ginkgo@latest
- name: Run tests
run: |
ginkgo -r -cover -covermode count --randomize-all --randomize-suites -coverprofile coverage.out --output-dir . -nodes 2
go test -shuffle on -race -coverprofile coverage.out ./...
- name: Convert Go coverage to LCOV
uses: jandelgado/[email protected]
with:
Expand Down
5 changes: 1 addition & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,9 @@ jobs:
- name: Install dependencies
run: |
go mod vendor
- name: Install Ginkgo
run: |
go install github.com/onsi/ginkgo/v2/ginkgo
- name: Run tests
run: |
ginkgo -r -cover -covermode count --randomize-all --randomize-suites -coverprofile coverage.out --output-dir . -nodes 2
go test -shuffle on -race -coverprofile coverage.out ./...
- name: Convert Go coverage to LCOV
uses: jandelgado/[email protected]
with:
Expand Down
11 changes: 8 additions & 3 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
---
# installation: `pre-commit install -t pre-commit -t commit-msg`
# installation: `pre-commit install`
default_install_hook_types:
- pre-commit
- commit-msg
repos:
- repo: https://github.com/talos-systems/conform
rev: v0.1.0-alpha.25
Expand All @@ -8,13 +11,15 @@ repos:
stages:
- commit-msg
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.1.0
rev: v4.4.0
hooks:
- id: check-merge-conflict
- id: end-of-file-fixer
- id: trailing-whitespace
args:
- "--markdown-linebreak-ext=md"
- repo: https://github.com/Yelp/detect-secrets
rev: v1.2.0
rev: v1.4.0
hooks:
- id: detect-secrets
exclude: go.*
Expand Down
1 change: 1 addition & 0 deletions .tool-versions
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pre-commit 3.4.0
10 changes: 5 additions & 5 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
1. Update the [README](README.md) with any changes to the interface if applicable.
2. Include unit tests where applicable (your code should not reduce test coverage).
3. All commit messages must follow [Conventional Commit](https://www.conventionalcommits.org/en/v1.0.0/) format.
4. Please submit Pull Requests from a feature branch in your fork, not from master.
5. Pull Requests with more than one commit will be rejected (squash your commits).
4. Please submit pull requests from a feature branch in your fork, not from `main` or `master`.

## pre-commit
If you want the best chance of having your Pull Request approved, install [pre-commit](https://pre-commit.com) and then run the following command in your local project directory:
## Pre-Commit

If you want the best chance of having your pull request approved, install [pre-commit](https://pre-commit.com) (if you use [asdf](https://asdf-vm.com) there's already a `.tool-versions` file for you) and then run the following command in your local project directory:

```sh
pre-commit install -t pre-commit -t commit-msg
pre-commit install
```

This will ensure that your code is likely to pass all checks.
15 changes: 0 additions & 15 deletions build/version_suite_test.go

This file was deleted.

218 changes: 124 additions & 94 deletions build/version_test.go
Original file line number Diff line number Diff line change
@@ -1,102 +1,132 @@
package build_test

import (
"io/ioutil"
"io"
"os"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"testing"

"github.com/particledecay/kconf/build"
. "github.com/particledecay/kconf/test"
)

var _ = Describe("Build/PrintVersion", func() {
It("Should print nothing if a version is not set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
build.Version = ""
build.PrintVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(out).To(BeEmpty())
})

It("Should print a version if it was set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
build.Version = "1.2.3"
build.PrintVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(string(out)).To(Equal("v1.2.3\n"))
})
})

var _ = Describe("Build/PrintLongVersion", func() {
It("Should print nothing if a version is not set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
build.Version = ""
build.PrintLongVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(out).To(BeEmpty())
})

It("Should print a version if it was set", func() {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

// all this function does is print to stdout
build.Version = "1.2.3"
build.Commit = "abcdef1234"
build.Date = "20200101"
build.PrintLongVersion()

// read captured stdout
w.Close()
out, _ := ioutil.ReadAll(r)

// restore stdout
os.Stdout = oldStdout

Expect(string(out)).To(ContainSubstring("Version:"))
Expect(string(out)).To(ContainSubstring("v1.2.3"))
Expect(string(out)).To(ContainSubstring("SHA:"))
Expect(string(out)).To(ContainSubstring("abcdef1234"))
Expect(string(out)).To(ContainSubstring("Built On:"))
Expect(string(out)).To(ContainSubstring("20200101"))
})
})
func TestPrintVersion(t *testing.T) {
var tests = map[string]func(*testing.T){
"print nothing if version not set": func(t *testing.T) {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

defer func() {
w.Close()
os.Stdout = oldStdout

// read captured stdout
out, _ := io.ReadAll(r)

if len(out) != 0 {
t.Errorf("expected: empty, got: %s", string(out))
}
}()

// all this function does is print to stdout
build.Version = ""
build.PrintVersion()
},
"print a version if set": func(t *testing.T) {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

defer func() {
w.Close()
os.Stdout = oldStdout

// read captured stdout
out, _ := io.ReadAll(r)

if string(out) != "v1.2.3\n" {
t.Errorf("expected: v1.2.3, got: %s", string(out))
}
}()

// all this function does is print to stdout
build.Version = "1.2.3"
build.PrintVersion()
},
}

for name, test := range tests {
t.Run(name, test)
}
}

func TestPrintLongVersion(t *testing.T) {
var tests = map[string]func(*testing.T){
"print nothing if version not set": func(t *testing.T) {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

defer func() {
w.Close()
os.Stdout = oldStdout

// read captured stdout
out, _ := io.ReadAll(r)

if len(out) != 0 {
t.Errorf("expected: empty, got: %s", string(out))
}
}()

// all this function does is print to stdout
build.Version = ""
err := build.PrintLongVersion()
if err != nil {
t.Errorf("expected: nil, got: %v", err)
}
},
"print a version if set": func(t *testing.T) {
// redirect stdout
oldStdout := os.Stdout
r, w, _ := os.Pipe()
os.Stdout = w

defer func() {
w.Close()
os.Stdout = oldStdout

// read captured stdout
out, _ := io.ReadAll(r)

// expected substrings
expected := []string{
"Version",
"v1.2.3",
"SHA",
"abcdef1234",
"Built On",
"20200101",
}

AssertSubstrings(t, out, expected)
}()

// all this function does is print to stdout
build.Version = "1.2.3"
build.Commit = "abcdef1234"
build.Date = "20200101"
err := build.PrintLongVersion()
if err != nil {
t.Errorf("expected: nil, got: %v", err)
}
},
}

for name, test := range tests {
t.Run(name, test)
}
}
Loading

0 comments on commit 4621b5f

Please sign in to comment.