aboutsummaryrefslogtreecommitdiff
path: root/internal/analyze/analyze_test.go
blob: 8aebf8d1cd79183a1ad62eeae0d10ac843080d26 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package analyze_test

import (
	"github.com/Fuwn/kivia/internal/analyze"
	"github.com/Fuwn/kivia/internal/collect"
	"os"
	"path/filepath"
	"testing"
)

func dictionaryPathForTests(testingContext *testing.T) string {
	testingContext.Helper()

	return filepath.Join("..", "..", "testdata", "dictionary", "words.txt")
}

func TestAnalyzeFlagsAbbreviations(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	root := filepath.Join("..", "..", "testdata", "samplepkg")
	identifiers, err := collect.FromPath(root)

	if err != nil {
		testingContext.Fatalf("collect.FromPath returned an error: %v", err)
	}

	result, err := analyze.Run(identifiers, analyze.Options{})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) == 0 {
		testingContext.Fatalf("Expected at least one violation, got none.")
	}

	mustContainViolation(testingContext, result, "ctx")
	mustContainViolation(testingContext, result, "userNum")
	mustContainViolation(testingContext, result, "usr")
}

func TestAnalyzeFlagsTechnicalTermsNotInDictionary(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	identifiers := []collect.Identifier{
		{Name: "userID", Kind: "variable"},
		{Name: "httpClient", Kind: "variable"},
	}
	result, err := analyze.Run(identifiers, analyze.Options{})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) == 0 {
		testingContext.Fatalf("Expected violations, got none.")
	}

	mustContainViolation(testingContext, result, "userID")
	mustContainViolation(testingContext, result, "httpClient")
}

func TestAnalyzeDoesNotFlagNormalDictionaryWords(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	identifiers := []collect.Identifier{
		{Name: "options", Kind: "variable"},
		{Name: "parsedResource", Kind: "variable"},
		{Name: "hasResources", Kind: "variable"},
		{Name: "allowlist", Kind: "variable"},
	}
	result, err := analyze.Run(identifiers, analyze.Options{})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) != 0 {
		testingContext.Fatalf("Expected no violations, got %d.", len(result.Violations))
	}
}

func TestAnalyzeMinEvaluationLengthSkipsSingleLetterIdentifiers(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	identifiers := []collect.Identifier{
		{Name: "t", Kind: "parameter"},
		{Name: "v", Kind: "receiver"},
		{Name: "ctx", Kind: "parameter"},
	}
	result, err := analyze.Run(identifiers, analyze.Options{
		MinEvaluationLength: 2,
	})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) != 1 {
		testingContext.Fatalf("Expected one violation, got %d.", len(result.Violations))
	}

	if result.Violations[0].Identifier.Name != "ctx" {
		testingContext.Fatalf("Expected only ctx to be evaluated, got %q.", result.Violations[0].Identifier.Name)
	}
}

func TestAnalyzeFlagsExpressionAbbreviation(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	identifiers := []collect.Identifier{
		{Name: "expr", Kind: "variable"},
	}
	result, err := analyze.Run(identifiers, analyze.Options{
		MinEvaluationLength: 1,
	})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) != 1 {
		testingContext.Fatalf("Expected one violation, got %d.", len(result.Violations))
	}

	if result.Violations[0].Identifier.Name != "expr" {
		testingContext.Fatalf("Expected expr to be flagged, got %q.", result.Violations[0].Identifier.Name)
	}
}

func TestAnalyzeAllowsUpperCaseTokens(testingContext *testing.T) {
	testingContext.Setenv("KIVIA_DICTIONARY_PATH", dictionaryPathForTests(testingContext))

	identifiers := []collect.Identifier{
		{Name: "JSON", Kind: "variable"},
	}
	result, err := analyze.Run(identifiers, analyze.Options{})

	if err != nil {
		testingContext.Fatalf("analyze.Run returned an error: %v", err)
	}

	if len(result.Violations) != 0 {
		testingContext.Fatalf("Expected no violations, got %d.", len(result.Violations))
	}
}

func TestAnalyzeFailsWhenDictionaryIsUnavailable(testingContext *testing.T) {
	emptyDictionaryPath := filepath.Join(testingContext.TempDir(), "empty.txt")

	if err := os.WriteFile(emptyDictionaryPath, []byte("\n"), 0o644); err != nil {
		testingContext.Fatalf("os.WriteFile returned an error: %v", err)
	}

	testingContext.Setenv("KIVIA_DICTIONARY_PATH", emptyDictionaryPath)

	_, err := analyze.Run([]collect.Identifier{{Name: "ctx", Kind: "parameter"}}, analyze.Options{})

	if err == nil {
		testingContext.Fatalf("Expected analyze.Run to fail when dictionary data is unavailable.")
	}
}

func mustContainViolation(testingContext *testing.T, result analyze.Result, name string) {
	testingContext.Helper()

	for _, violation := range result.Violations {
		if violation.Identifier.Name == name {
			return
		}
	}

	testingContext.Fatalf("Expected a violation for %q.", name)
}