1+ #! /bin/bash
2+ set -e
3+
4+ # This script tests compatibility of goconst with golangci-lint
5+ # using the command line interface rather than importing the package
6+
7+ echo " Testing compatibility with golangci-lint..."
8+
9+ # Get current directory
10+ REPO_ROOT=" $( pwd) "
11+ echo " Using repository at: $REPO_ROOT "
12+
13+ # Set up test directory
14+ TEST_DIR=$( mktemp -d)
15+ echo " Created test directory: $TEST_DIR "
16+
17+ # Clean up on exit
18+ cleanup () {
19+ echo " Cleaning up test directory..."
20+ rm -rf " $TEST_DIR "
21+ }
22+ trap cleanup EXIT
23+
24+ # Build goconst CLI
25+ echo " Building goconst CLI..."
26+ GOCONST_BIN=" $TEST_DIR /goconst"
27+ go build -o " $GOCONST_BIN " ./cmd/goconst
28+
29+ # Create test files
30+ echo " Creating test files..."
31+ mkdir -p " $TEST_DIR /testpkg"
32+
33+ # Create a file with constants and strings
34+ cat > " $TEST_DIR /testpkg/main.go" << 'EOF '
35+ package testpkg
36+
37+ const ExistingConst = "test-const"
38+
39+ func example() {
40+ // This should be detected as it matches ExistingConst
41+ str1 := "test-const"
42+ str2 := "test-const"
43+
44+ // This should be detected as a duplicate without matching constant
45+ dup1 := "duplicate"
46+ dup2 := "duplicate"
47+
48+ // This should be ignored due to ignore-strings
49+ skip := "test-ignore"
50+ skip2 := "test-ignore"
51+ }
52+ EOF
53+
54+ # Test 1: Basic functionality
55+ echo " Test 1: Basic functionality (without match-with-constants)..."
56+ " $GOCONST_BIN " -ignore-strings " test-ignore" -match-constant=false " $TEST_DIR /testpkg" > " $TEST_DIR /output1.txt"
57+ if ! grep -q " duplicate" " $TEST_DIR /output1.txt" ; then
58+ echo " Failed: Should detect 'duplicate' string"
59+ cat " $TEST_DIR /output1.txt"
60+ exit 1
61+ fi
62+ if ! grep -q " test-const" " $TEST_DIR /output1.txt" ; then
63+ echo " Failed: Should detect 'test-const' string"
64+ cat " $TEST_DIR /output1.txt"
65+ exit 1
66+ fi
67+ if grep -q " test-ignore" " $TEST_DIR /output1.txt" ; then
68+ echo " Failed: Should NOT detect 'test-ignore' string"
69+ cat " $TEST_DIR /output1.txt"
70+ exit 1
71+ fi
72+
73+ # Test 2: Match with constants
74+ echo " Test 2: Testing match-with-constants functionality..."
75+ " $GOCONST_BIN " -ignore-strings " test-ignore" -match-constant " $TEST_DIR /testpkg" > " $TEST_DIR /output2.txt"
76+ if ! grep -q " matching constant.*ExistingConst" " $TEST_DIR /output2.txt" ; then
77+ echo " Failed: Should match 'test-const' with 'ExistingConst'"
78+ cat " $TEST_DIR /output2.txt"
79+ exit 1
80+ fi
81+ if ! grep -q " duplicate" " $TEST_DIR /output2.txt" ; then
82+ echo " Failed: Should detect 'duplicate' string"
83+ cat " $TEST_DIR /output2.txt"
84+ exit 1
85+ fi
86+ if grep -q " test-ignore" " $TEST_DIR /output2.txt" ; then
87+ echo " Failed: Should NOT detect 'test-ignore' string"
88+ cat " $TEST_DIR /output2.txt"
89+ exit 1
90+ fi
91+
92+ # Test 3: Create another test file with multiple constants
93+ cat > " $TEST_DIR /testpkg/multi_const.go" << 'EOF '
94+ package testpkg
95+
96+ const (
97+ FirstConst = "duplicate-value"
98+ SecondConst = "duplicate-value"
99+ )
100+
101+ func multipleConstants() {
102+ x := "duplicate-value"
103+ y := "duplicate-value"
104+ }
105+ EOF
106+
107+ echo " Test 3: Testing multiple constants with same value..."
108+ " $GOCONST_BIN " -match-constant " $TEST_DIR /testpkg/multi_const.go" > " $TEST_DIR /output3.txt"
109+ if ! grep -q " matching constant.*FirstConst" " $TEST_DIR /output3.txt" ; then
110+ echo " Failed: Should match 'duplicate-value' with 'FirstConst'"
111+ cat " $TEST_DIR /output3.txt"
112+ exit 1
113+ fi
114+
115+ # Test 4: Test with JSON output (golangci-lint compatibility)
116+ echo " Test 4: Testing JSON output format..."
117+ " $GOCONST_BIN " -ignore-strings " test-ignore" -match-constant -output json " $TEST_DIR /testpkg/main.go" > " $TEST_DIR /output4.json"
118+ # Check that the JSON has the correct structure: strings + constants sections
119+ if ! grep -q ' "constants":{"test-const":\[.*"Name":"ExistingConst"' " $TEST_DIR /output4.json" ; then
120+ echo " Failed: JSON output should include constants with ExistingConst"
121+ cat " $TEST_DIR /output4.json"
122+ exit 1
123+ fi
124+
125+ echo " All compatibility tests PASSED!"
0 commit comments