Skip to content

Commit ad5a20e

Browse files
authored
test: adding more tests and bumping the cli version (#11)
1 parent 923d1a2 commit ad5a20e

File tree

17 files changed

+672
-280
lines changed

17 files changed

+672
-280
lines changed

.github/workflows/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ jobs:
1919
with:
2020
go-version-file: go.mod
2121

22-
- run: go test ./...
22+
- run: make test
2323

2424
lint:
2525
runs-on: ubuntu-latest

Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ help: ## Show this help.
88
build: ## Build the LSP binary
99
go build -o bin/infracost-ls .
1010

11+
.PHONY: test
12+
test: ## Run tests
13+
go test -v ./...
14+
1115
.PHONY: lint
1216
lint: lint_install ## Run linting operations
1317
golangci-lint run ./...

go.mod

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,33 @@ require (
66
github.com/hashicorp/go-hclog v1.6.3
77
github.com/hashicorp/go-plugin v1.7.0 // indirect
88
github.com/infracost/config v0.3.15
9-
github.com/infracost/go-proto v0.2.1
9+
github.com/infracost/go-proto v0.2.2
1010
github.com/infracost/proto v1.21.0
1111
github.com/owenrumney/go-lsp v0.0.8
1212
)
1313

1414
require (
15-
github.com/infracost/cli v0.0.5
15+
github.com/infracost/cli v0.0.6
16+
github.com/stretchr/testify v1.11.1
1617
golang.org/x/oauth2 v0.35.0
1718
google.golang.org/grpc v1.78.0
1819
google.golang.org/protobuf v1.36.11
1920
)
2021

2122
require (
23+
github.com/Masterminds/semver/v3 v3.4.0 // indirect
2224
github.com/MicahParks/jwkset v0.11.0 // indirect
2325
github.com/MicahParks/keyfunc/v3 v3.7.0 // indirect
2426
github.com/agext/levenshtein v1.2.3 // indirect
2527
github.com/agnivade/levenshtein v1.2.1 // indirect
2628
github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect
29+
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
2730
github.com/dlclark/regexp2 v1.2.0 // indirect
2831
github.com/fatih/color v1.18.0 // indirect
2932
github.com/gobwas/glob v0.2.3 // indirect
3033
github.com/golang-jwt/jwt/v5 v5.3.0 // indirect
3134
github.com/golang/protobuf v1.5.4 // indirect
35+
github.com/google/uuid v1.6.0 // indirect
3236
github.com/gorilla/websocket v1.5.3 // indirect
3337
github.com/hashicorp/hcl/v2 v2.24.0 // indirect
3438
github.com/hashicorp/yamux v0.1.2 // indirect
@@ -39,11 +43,11 @@ require (
3943
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
4044
github.com/modern-go/reflect2 v1.0.2 // indirect
4145
github.com/oklog/run v1.1.0 // indirect
46+
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
4247
github.com/rogpeppe/go-internal v1.9.0 // indirect
4348
github.com/rs/zerolog v1.34.0 // indirect
4449
github.com/soongo/path-to-regexp v1.6.4 // indirect
4550
github.com/spf13/pflag v1.0.9 // indirect
46-
github.com/stretchr/testify v1.11.1 // indirect
4751
github.com/zclconf/go-cty v1.17.0 // indirect
4852
go.opentelemetry.io/otel v1.42.0 // indirect
4953
go.opentelemetry.io/otel/sdk/metric v1.42.0 // indirect

go.sum

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/Masterminds/semver/v3 v3.4.0 h1:Zog+i5UMtVoCU8oKka5P7i9q9HgrJeGzI9SA1Xbatp0=
2+
github.com/Masterminds/semver/v3 v3.4.0/go.mod h1:4V+yj/TJE1HU9XfppCwVMZq3I84lprf4nC11bSS5beM=
13
github.com/MicahParks/jwkset v0.11.0 h1:yc0zG+jCvZpWgFDFmvs8/8jqqVBG9oyIbmBtmjOhoyQ=
24
github.com/MicahParks/jwkset v0.11.0/go.mod h1:U2oRhRaLgDCLjtpGL2GseNKGmZtLs/3O7p+OZaL5vo0=
35
github.com/MicahParks/keyfunc/v3 v3.7.0 h1:pdafUNyq+p3ZlvjJX1HWFP7MA3+cLpDtg69U3kITJGM=
@@ -54,12 +56,12 @@ github.com/hashicorp/hcl/v2 v2.24.0 h1:2QJdZ454DSsYGoaE6QheQZjtKZSUs9Nh2izTWiwQx
5456
github.com/hashicorp/hcl/v2 v2.24.0/go.mod h1:oGoO1FIQYfn/AgyOhlg9qLC6/nOJPX3qGbkZpYAcqfM=
5557
github.com/hashicorp/yamux v0.1.2 h1:XtB8kyFOyHXYVFnwT5C3+Bdo8gArse7j2AQ0DA0Uey8=
5658
github.com/hashicorp/yamux v0.1.2/go.mod h1:C+zze2n6e/7wshOZep2A70/aQU6QBRWJO/G6FT1wIns=
57-
github.com/infracost/cli v0.0.5 h1:plJrLXJEEGKTfYGIQcvV3tLkkKEPY8tv4w5Rr1YEK9w=
58-
github.com/infracost/cli v0.0.5/go.mod h1:zeBXu4NybpOy0KqL4RWib+eXSUMX1oKrWvj60IWsi4Y=
59+
github.com/infracost/cli v0.0.6 h1:o0MMz3sgGCT7kvl+Ms+NneiQ615XNOmbiJFrjv2zyiI=
60+
github.com/infracost/cli v0.0.6/go.mod h1:zeBXu4NybpOy0KqL4RWib+eXSUMX1oKrWvj60IWsi4Y=
5961
github.com/infracost/config v0.3.15 h1:e08GIGvKFgjh+lzMAgnpNIScPhuLAZEHsS9JnJsPQaA=
6062
github.com/infracost/config v0.3.15/go.mod h1:8tXgHnaQvE9hhwdjaa3sRF3K4hmIaIcPNF/iaouHjqc=
61-
github.com/infracost/go-proto v0.2.1 h1:NrM4z6D6rmkMkhaU7JqblG3imCmKenlB6YW9gmhNc10=
62-
github.com/infracost/go-proto v0.2.1/go.mod h1:C8q18V30lfDaVgRKoGeo3NN+CridCKkzbEjxCovQcfk=
63+
github.com/infracost/go-proto v0.2.2 h1:shX1EIm4K1mFThSNXKg2/ktluZgqt0bbZ91mBNazS8s=
64+
github.com/infracost/go-proto v0.2.2/go.mod h1:C8q18V30lfDaVgRKoGeo3NN+CridCKkzbEjxCovQcfk=
6365
github.com/infracost/proto v1.21.0 h1:P7CQQp9CSK8Yt+V2imtOzuopGjBFJleij6EK25Owpd8=
6466
github.com/infracost/proto v1.21.0/go.mod h1:BO6ew0skDYqM8lxl3rKKMA7WRM7VG1dIUKErbTdfGPk=
6567
github.com/jhump/protoreflect v1.17.0 h1:qOEr613fac2lOuTgWN4tPAtLL7fUSbuJL5X5XumQh94=

internal/ignore/store_test.go

Lines changed: 28 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,27 +4,24 @@ import (
44
"os"
55
"path/filepath"
66
"testing"
7+
8+
"github.com/stretchr/testify/assert"
9+
"github.com/stretchr/testify/require"
710
)
811

912
func TestKey(t *testing.T) {
1013
k1 := Key("/a/b.tf", "aws_instance.web", "aws-gp3-volumes")
1114
k2 := Key("/a/b.tf", "aws_instance.web", "aws-gp3-volumes")
1215
k3 := Key("/a/c.tf", "aws_instance.web", "aws-gp3-volumes")
1316

14-
if k1 != k2 {
15-
t.Fatal("identical inputs should produce identical keys")
16-
}
17-
if k1 == k3 {
18-
t.Fatal("different inputs should produce different keys")
19-
}
17+
assert.Equal(t, k1, k2, "identical inputs should produce identical keys")
18+
assert.NotEqual(t, k1, k3, "different inputs should produce different keys")
2019
}
2120

2221
func TestStoreAddAndIsIgnored(t *testing.T) {
2322
path := filepath.Join(t.TempDir(), "ignores.json")
2423
s, err := NewStoreWithPath(path)
25-
if err != nil {
26-
t.Fatal(err)
27-
}
24+
require.NoError(t, err)
2825

2926
tests := []struct {
3027
name string
@@ -82,13 +79,10 @@ func TestStoreAddAndIsIgnored(t *testing.T) {
8279
t.Run(tt.name, func(t *testing.T) {
8380
// Fresh store per test case.
8481
s.ignores = make(map[string]Entry)
85-
if err := s.Add(tt.addPath, tt.addRes, tt.addSlug); err != nil {
86-
t.Fatal(err)
87-
}
82+
err := s.Add(tt.addPath, tt.addRes, tt.addSlug)
83+
require.NoError(t, err)
8884
got := s.IsIgnored(tt.queryP, tt.queryR, tt.queryS)
89-
if got != tt.expected {
90-
t.Errorf("IsIgnored(%q, %q, %q) = %v, want %v", tt.queryP, tt.queryR, tt.queryS, got, tt.expected)
91-
}
85+
assert.Equal(t, tt.expected, got)
9286
})
9387
}
9488
}
@@ -97,70 +91,47 @@ func TestStorePersistence(t *testing.T) {
9791
path := filepath.Join(t.TempDir(), "ignores.json")
9892

9993
s1, err := NewStoreWithPath(path)
100-
if err != nil {
101-
t.Fatal(err)
102-
}
103-
if err := s1.Add("/a/b.tf", "aws_instance.web", "slug1"); err != nil {
104-
t.Fatal(err)
105-
}
94+
require.NoError(t, err)
95+
err = s1.Add("/a/b.tf", "aws_instance.web", "slug1")
96+
require.NoError(t, err)
10697

10798
// Load a new store from the same file.
10899
s2, err := NewStoreWithPath(path)
109-
if err != nil {
110-
t.Fatal(err)
111-
}
112-
if !s2.IsIgnored("/a/b.tf", "aws_instance.web", "slug1") {
113-
t.Fatal("expected ignore to persist across store instances")
114-
}
100+
require.NoError(t, err)
101+
assert.True(t, s2.IsIgnored("/a/b.tf", "aws_instance.web", "slug1"), "expected ignore to persist across store instances")
115102
}
116103

117104
func TestStoreRemove(t *testing.T) {
118105
path := filepath.Join(t.TempDir(), "ignores.json")
119106
s, err := NewStoreWithPath(path)
120-
if err != nil {
121-
t.Fatal(err)
122-
}
107+
require.NoError(t, err)
123108

124-
if err := s.Add("/a/b.tf", "res", "slug"); err != nil {
125-
t.Fatal(err)
126-
}
127-
if !s.IsIgnored("/a/b.tf", "res", "slug") {
128-
t.Fatal("expected ignored after add")
129-
}
109+
err = s.Add("/a/b.tf", "res", "slug")
110+
require.NoError(t, err)
111+
assert.True(t, s.IsIgnored("/a/b.tf", "res", "slug"), "expected ignored after add")
130112

131113
key := Key("/a/b.tf", "res", "slug")
132-
if err := s.Remove(key); err != nil {
133-
t.Fatal(err)
134-
}
135-
if s.IsIgnored("/a/b.tf", "res", "slug") {
136-
t.Fatal("expected not ignored after remove")
137-
}
114+
err = s.Remove(key)
115+
require.NoError(t, err)
116+
assert.False(t, s.IsIgnored("/a/b.tf", "res", "slug"), "expected not ignored after remove")
138117
}
139118

140119
func TestNewStoreMissingFile(t *testing.T) {
141120
path := filepath.Join(t.TempDir(), "does-not-exist", "ignores.json")
142121
s, err := NewStoreWithPath(path)
143-
if err != nil {
144-
t.Fatal("missing file should not error", err)
145-
}
146-
if s.IsIgnored("/a", "b", "c") {
147-
t.Fatal("empty store should not match anything")
148-
}
122+
require.NoError(t, err, "missing file should not error")
123+
assert.False(t, s.IsIgnored("/a", "b", "c"), "empty store should not match anything")
149124
}
150125

151126
func TestNewStoreEnvOverride(t *testing.T) {
152127
path := filepath.Join(t.TempDir(), "custom-ignores.json")
153128
t.Setenv("INFRACOST_IGNORES_FILE", path)
154129

155130
s, err := NewStore()
156-
if err != nil {
157-
t.Fatal(err)
158-
}
159-
if err := s.Add("/a", "b", "c"); err != nil {
160-
t.Fatal(err)
161-
}
131+
require.NoError(t, err)
132+
err = s.Add("/a", "b", "c")
133+
require.NoError(t, err)
162134

163-
if _, err := os.Stat(path); err != nil {
164-
t.Fatalf("expected file at env override path: %v", err)
165-
}
135+
_, err = os.Stat(path)
136+
require.NoError(t, err, "expected file at env override path")
166137
}

internal/lsp/analyzer_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"time"
77

88
repoconfig "github.com/infracost/config"
9+
"github.com/stretchr/testify/assert"
910
)
1011

1112
func TestScheduleAnalyzeDebounce(t *testing.T) {
@@ -49,9 +50,7 @@ func TestScheduleAnalyzeDebounce(t *testing.T) {
4950
// Wait for debounce to fire (last save + debounce window + margin).
5051
time.Sleep(scanDebounce + 100*time.Millisecond)
5152

52-
if got := scanCount.Load(); got != 1 {
53-
t.Errorf("expected 1 scan, got %d", got)
54-
}
53+
assert.Equal(t, int32(1), scanCount.Load())
5554
}
5655

5756
func TestScheduleAnalyzeCoalescesRapidSaves(t *testing.T) {
@@ -74,9 +73,7 @@ func TestScheduleAnalyzeCoalescesRapidSaves(t *testing.T) {
7473
timerCount := len(srv.scanTimers)
7574
srv.mu.RUnlock()
7675

77-
if timerCount != 1 {
78-
t.Errorf("expected 1 pending timer, got %d", timerCount)
79-
}
76+
assert.Equal(t, 1, timerCount)
8077

8178
// Clean up: stop timers so the callback doesn't fire and panic.
8279
srv.mu.Lock()
@@ -123,6 +120,6 @@ func TestScheduleAnalyzeCancelsInFlight(t *testing.T) {
123120
case <-cancelled:
124121
// The old in-flight scan was cancelled.
125122
case <-time.After(time.Second):
126-
t.Error("expected in-flight scan to be cancelled")
123+
assert.Fail(t, "expected in-flight scan to be cancelled")
127124
}
128125
}

0 commit comments

Comments
 (0)