Skip to content

Commit e02c380

Browse files
authored
Merge pull request #36 from brunopadz/chore/write-tests
chore: write tests
2 parents 6f32492 + 60fdb28 commit e02c380

File tree

6 files changed

+80
-10
lines changed

6 files changed

+80
-10
lines changed

.github/workflows/tests.yml

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
name: Run tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
lint:
9+
name: Check lint
10+
runs-on: ubuntu-20.04
11+
steps:
12+
- name: Checkout
13+
uses: actions/checkout@v2
14+
15+
- uses: reviewdog/action-staticcheck@v1
16+
with:
17+
github_token: ${{ secrets.GITHUB_TOKEN }}
18+
reporter: github-pr-review
19+
filter_mode: nofilter
20+
fail_on_error: true
21+
22+
tests:
23+
name: Run tests
24+
runs-on: ubuntu-20.04
25+
steps:
26+
- name: Checkout
27+
uses: actions/checkout@v2
28+
-
29+
name: Set up
30+
uses: actions/setup-go@v2
31+
with:
32+
go-version: 1.16
33+
-
34+
name: Download modules
35+
run: go mod download
36+
-
37+
name: Unit tests
38+
run: go test ./pkg/...
39+
-
40+
name: Generate coverage
41+
run: go test -coverpkg=./pkg/... -coverprofile=coverage.out ./pkg/...

cmd/awsInspect.go

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@ import (
55
"fmt"
66
"log"
77

8-
"github.com/brunopadz/amictl/pkg/utils"
9-
10-
"github.com/pterm/pterm"
11-
128
"github.com/aws/aws-sdk-go-v2/aws"
139
"github.com/aws/aws-sdk-go-v2/service/ec2"
1410
cfg "github.com/brunopadz/amictl/config"
1511
aaws "github.com/brunopadz/amictl/pkg/providers/aws"
12+
"github.com/brunopadz/amictl/pkg/utils"
13+
"github.com/pterm/pterm"
1614
"github.com/spf13/cobra"
1715
"github.com/spf13/viper"
1816
)

cmd/awsListUnused.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import (
99
"github.com/aws/aws-sdk-go-v2/service/ec2"
1010
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
1111
cfg "github.com/brunopadz/amictl/config"
12-
"github.com/brunopadz/amictl/pkg/commons"
1312
aaws "github.com/brunopadz/amictl/pkg/providers/aws"
13+
"github.com/brunopadz/amictl/pkg/utils"
1414
"github.com/pterm/pterm"
1515
"github.com/spf13/cobra"
1616
"github.com/spf13/viper"
@@ -92,7 +92,7 @@ func runUnused(cmd *cobra.Command, _ []string) error {
9292
}
9393
}
9494

95-
x = commons.Compare(AllImages, UsedImages)
95+
x = utils.Compare(AllImages, UsedImages)
9696

9797
for _, id := range x {
9898
d = append(d, []string{id, v})
@@ -104,13 +104,13 @@ func runUnused(cmd *cobra.Command, _ []string) error {
104104

105105
err = pterm.DefaultTable.WithHasHeader().WithData(d).Render()
106106

107-
if (l <= 20){
107+
if l <= 20 {
108108
pterm.Println(pterm.Green(fmt.Sprintf("%d AMIs are not being utilized.", l)))
109109
} else if (l > 20) && (l < 50) {
110110
pterm.Println(pterm.Yellow(fmt.Sprintf("%d AMIs are not being utilized.", l)))
111-
} else if ( l >= 50) {
111+
} else if l >= 50 {
112112
pterm.Println(pterm.Red(fmt.Sprintf("%d AMIs are not being utilized.", l)))
113113
}
114-
114+
115115
return nil
116116
}

pkg/commons/compare.go pkg/utils/compare.go

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
package commons
1+
package utils
22

3+
// Compare returns a slice of string with values that are not duplicated
4+
// by comparing two slice of strings
35
func Compare(a, b []string) []string {
46
for i := len(a) - 1; i >= 0; i-- {
57
for _, v := range b {

pkg/utils/compare_test.go

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestCompare(t *testing.T) {
8+
a := []string{"foo", "bar", "baz"}
9+
b := []string{"bar", "baz"}
10+
11+
r := Compare(a, b)
12+
if r[0] != a[0] {
13+
t.Errorf("Compare(a, b) = %v,; want \"foo\"", Compare(a, b))
14+
}
15+
}

pkg/utils/empty_test.go

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package utils
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestEmptyString(t *testing.T) {
8+
a := ""
9+
10+
if EmptyString(a) != "-" && len(a) == 0 {
11+
t.Errorf("EmptyString(\"\") = %v; want \"-\"", EmptyString(a))
12+
}
13+
14+
}

0 commit comments

Comments
 (0)