Skip to content

Commit f1ef3bc

Browse files
clean: Remove global variables and move test resources folder
1 parent 9d11956 commit f1ef3bc

13 files changed

+128
-53
lines changed

README.md

-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,6 @@ Then, on the main function you just need to instantiate it and pass to engine's
3030
As an example:
3131

3232
```
33-
34-
3533
// GoTool tool implementation
3634
type GoTool struct {
3735
}

configuration_test.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@ package codacytool
22

33
import (
44
"github.com/stretchr/testify/assert"
5+
"path/filepath"
56
"testing"
67
)
78

9+
const configurationTestResources = "configuration"
10+
811
func TestParseConfiguration(t *testing.T) {
9-
location := testsResourcesLocation + "/.codacyrc"
12+
location := filepath.Join(testsResourcesLocation, configurationTestResources, ".codacyrc.valid")
1013

1114
configuration, err := ParseConfiguration(location)
1215

@@ -16,3 +19,28 @@ func TestParseConfiguration(t *testing.T) {
1619
got := len(configuration.Files)
1720
assert.Equal(t, expected, got)
1821
}
22+
23+
func TestParseInvalidConfiguration(t *testing.T) {
24+
location := filepath.Join(testsResourcesLocation, configurationTestResources, ".codacyrc.invalid")
25+
configuration, err := ParseConfiguration(location)
26+
27+
assert.Nil(t, err)
28+
29+
// Files is present
30+
expected := 2
31+
got := len(configuration.Files)
32+
assert.Equal(t, expected, got)
33+
34+
// Tools has a typo, so it must not parse it and return 0 tools
35+
expected = 0
36+
got = len(configuration.Tools)
37+
assert.Equal(t, expected, got)
38+
}
39+
40+
func TestParseNonExistingConfiguration(t *testing.T) {
41+
location := filepath.Join(testsResourcesLocation, configurationTestResources, ".codacyrc.notexists")
42+
43+
_, err := ParseConfiguration(location)
44+
45+
assert.NotNil(t, err)
46+
}

configurationfiles.go

+13-13
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,30 @@
11
package codacytool
22

3+
import (
4+
"path/filepath"
5+
)
6+
37
const (
48
toolFilesBasePath = "/"
59
defaultDefinitionFileName = "docs/patterns.json"
610
defaultConfigFileName = ".codacyrc"
711
)
812

9-
func getBasePath() string {
10-
var basePathFromEnv string
11-
if toolConfigsBasePathFlag != nil {
12-
basePathFromEnv = *toolConfigsBasePathFlag
13-
}
14-
if basePathFromEnv != "" {
15-
return basePathFromEnv
13+
func getBasePath(toolConfigsBasePath string) string {
14+
if toolConfigsBasePath != "" {
15+
return toolConfigsBasePath
1616
}
1717
return toolFilesBasePath
1818
}
1919

20-
func getPathToFile(file string) string {
21-
return getBasePath() + file
20+
func getPathToFile(toolConfigsBasePath string, file string) string {
21+
return filepath.Join(getBasePath(toolConfigsBasePath), file)
2222
}
2323

24-
func defaultDefinitionFile() string {
25-
return getPathToFile(defaultDefinitionFileName)
24+
func defaultDefinitionFile(toolConfigsBasePath string) string {
25+
return getPathToFile(toolConfigsBasePath, defaultDefinitionFileName)
2626
}
2727

28-
func defaultConfigurationFile() string {
29-
return getPathToFile(defaultConfigFileName)
28+
func defaultConfigurationFile(toolConfigsBasePath string) string {
29+
return getPathToFile(toolConfigsBasePath, defaultConfigFileName)
3030
}

configurationfiles_test.go

+10-7
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,32 @@ package codacytool
22

33
import (
44
"github.com/stretchr/testify/assert"
5+
"path/filepath"
56
"testing"
67
)
78

9+
const basePath = "/"
10+
811
func TestGetBasePath(t *testing.T) {
9-
basePath := getBasePath()
12+
basePath := getBasePath(basePath)
1013
assert.Equal(t, toolFilesBasePath, basePath)
1114
}
1215

1316
func TestGetPathToFile(t *testing.T) {
1417
file := "/file"
15-
expectedPath := getBasePath() + file
16-
path := getPathToFile(file)
18+
expectedPath := filepath.Join(getBasePath(basePath), file)
19+
path := getPathToFile(basePath, file)
1720
assert.Equal(t, expectedPath, path)
1821
}
1922

2023
func TestDefaultDefinitionFile(t *testing.T) {
21-
expectedPath := getPathToFile(defaultDefinitionFileName)
22-
path := defaultDefinitionFile()
24+
expectedPath := getPathToFile(basePath, defaultDefinitionFileName)
25+
path := defaultDefinitionFile(basePath)
2326
assert.Equal(t, expectedPath, path)
2427
}
2528

2629
func TestDefaultConfigurationFile(t *testing.T) {
27-
expectedPath := getPathToFile(defaultConfigFileName)
28-
path := defaultConfigurationFile()
30+
expectedPath := getPathToFile(basePath, defaultConfigFileName)
31+
path := defaultConfigurationFile(basePath)
2932
assert.Equal(t, expectedPath, path)
3033
}

global_tests.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
package codacytool
22

3-
var testsResourcesLocation = "./resources/tests/"
3+
var testsResourcesLocation = "./tests/"

go.sum

+1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

init.go

+19-13
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,40 @@ import (
77
"time"
88
)
99

10-
var (
11-
sourceDirFlag *string
12-
toolConfigsBasePathFlag *string
13-
)
10+
// RunConfiguration contains the process run configuration
11+
type RunConfiguration struct {
12+
sourceDir string
13+
toolConfigsBasePath string
14+
timeoutDuration time.Duration
15+
}
1416

1517
const (
1618
defaultTimeout = 15 * time.Minute
1719
)
1820

1921
// StartTool receives the tool implementation as parameter and run the tool
2022
func StartTool(impl ToolImplementation) {
21-
parseFlags()
23+
cmdLineConfig := parseFlags()
2224

23-
runWithTimeout(impl, timeoutSeconds())
25+
runWithTimeout(impl, cmdLineConfig)
2426
}
2527

26-
func runWithTimeout(impl ToolImplementation, maxDuration time.Duration) {
28+
func runWithTimeout(impl ToolImplementation, runConfiguration RunConfiguration) {
2729
runMethodWithTimeout(func() {
28-
startToolImplementation(impl, *sourceDirFlag)
30+
startToolImplementation(impl, runConfiguration)
2931
}, func() {
30-
fmt.Fprintf(os.Stderr, "Timeout of %s seconds exceeded", maxDuration)
32+
fmt.Fprintf(os.Stderr, "Timeout of %s seconds exceeded", runConfiguration.timeoutDuration)
3133
os.Exit(1)
32-
}, maxDuration)
34+
}, runConfiguration.timeoutDuration)
3335
}
3436

35-
func parseFlags() {
36-
sourceDirFlag = flag.String("sourceDir", "/src", "source to analyse folder")
37-
toolConfigsBasePathFlag = flag.String("toolConfigLocation", "/", "Location of tool configuration")
37+
func parseFlags() RunConfiguration {
38+
cmdLineConfig := RunConfiguration{
39+
sourceDir: *flag.String("sourceDir", "/src", "source to analyse folder"),
40+
toolConfigsBasePath: *flag.String("toolConfigLocation", "/", "Location of tool configuration"),
41+
timeoutDuration: timeoutSeconds(),
42+
}
3843

3944
flag.Parse()
45+
return cmdLineConfig
4046
}

tests/configuration/.codacyrc.invalid

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"files" : ["foo/bar/baz.go", "foo2/bar/baz.go"],
3+
"tool":[
4+
{
5+
"name":"govet",
6+
"patterns":[
7+
{
8+
"patternId":"govet",
9+
"parameters":[
10+
{
11+
"name":"govet",
12+
"value":"vars"
13+
}
14+
]
15+
}
16+
]
17+
}
18+
]
19+
}
File renamed without changes.

tests/tool/.codacyrc

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"files" : ["foo/bar/baz.go", "foo2/bar/baz.go"],
3+
"tools":[
4+
{
5+
"name":"govet",
6+
"patterns":[
7+
{
8+
"patternId":"govet",
9+
"parameters":[
10+
{
11+
"name":"govet",
12+
"value":"vars"
13+
}
14+
]
15+
}
16+
]
17+
}
18+
]
19+
}
File renamed without changes.

tool.go

+7-7
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ func newTool(toolDefinition ToolDefinition, config Configuration) Tool {
3737
}
3838
}
3939

40-
func defaultTool() Tool {
41-
toolDefinition, err := LoadToolDefinition(defaultDefinitionFile())
40+
func defaultTool(runConfig RunConfiguration) Tool {
41+
toolDefinition, err := LoadToolDefinition(defaultDefinitionFile(runConfig.toolConfigsBasePath))
4242
if err != nil {
4343
panic(err)
4444
}
45-
config, err := ParseConfiguration(defaultConfigurationFile())
45+
config, err := ParseConfiguration(defaultConfigurationFile(runConfig.toolConfigsBasePath))
4646
if err != nil {
47-
logrus.Debug(defaultConfigurationFile() + " parsing error: " + err.Error())
47+
logrus.Debug(defaultConfigurationFile(runConfig.toolConfigsBasePath) + " parsing error: " + err.Error())
4848
}
4949

5050
return newTool(toolDefinition, config)
@@ -79,10 +79,10 @@ func printResult(issues []Issue) {
7979
logrus.Info(printResult)
8080
}
8181

82-
func startToolImplementation(impl ToolImplementation, sourceDir string) {
83-
tool := defaultTool()
82+
func startToolImplementation(impl ToolImplementation, runConfiguration RunConfiguration) {
83+
tool := defaultTool(runConfiguration)
8484

85-
result, err := impl.Run(tool, sourceDir)
85+
result, err := impl.Run(tool, runConfiguration.sourceDir)
8686
if err != nil {
8787
logrus.Errorln(err.Error())
8888
os.Exit(1)

tool_test.go

+10-9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
logrus "github.com/sirupsen/logrus"
66
"github.com/stretchr/testify/assert"
77
"github.com/stretchr/testify/suite"
8+
"path/filepath"
89

910
"io/ioutil"
1011

@@ -15,14 +16,18 @@ import (
1516

1617
type ToolTestSuite struct {
1718
suite.Suite
19+
runConfig RunConfiguration
1820
}
1921

2022
func TestExampleTestSuite(t *testing.T) {
2123
suite.Run(t, new(ToolTestSuite))
2224
}
2325

2426
func (suite *ToolTestSuite) SetupTest() {
25-
toolConfigsBasePathFlag = &testsResourcesLocation
27+
suite.runConfig = RunConfiguration{
28+
toolConfigsBasePath: filepath.Join(testsResourcesLocation, "tool"),
29+
sourceDir: "./",
30+
}
2631
}
2732

2833
func testingTool(name, version string) (ToolDefinition, string) {
@@ -58,7 +63,7 @@ func (suite *ToolTestSuite) TestToolToJSONWithoutVersion() {
5863
}
5964

6065
func (suite *ToolTestSuite) TestLoadToolDefinition() {
61-
patternsFileLocation := defaultDefinitionFile()
66+
patternsFileLocation := defaultDefinitionFile(suite.runConfig.toolConfigsBasePath)
6267
tool, err := LoadToolDefinition(patternsFileLocation)
6368

6469
assert.Nil(suite.T(), err, "Failed to load tool %s", patternsFileLocation)
@@ -94,7 +99,7 @@ func (suite *ToolTestSuite) TestPrintResults() {
9499
}
95100

96101
func (suite *ToolTestSuite) TestDefaultTool() {
97-
tool := defaultTool()
102+
tool := defaultTool(suite.runConfig)
98103

99104
patternsLen := len(tool.Patterns)
100105
expectedLen := 1
@@ -105,7 +110,7 @@ func (suite *ToolTestSuite) TestDefaultTool() {
105110
}
106111
func (suite *ToolTestSuite) TestPatternsFromConfig() {
107112
toolName := "govet"
108-
configFile := defaultConfigurationFile()
113+
configFile := defaultConfigurationFile(suite.runConfig.toolConfigsBasePath)
109114
config, err := ParseConfiguration(configFile)
110115
assert.Nil(suite.T(), err, "Error parsing config file %s", configFile)
111116

@@ -129,12 +134,8 @@ func (i ToolImplementationTest) Run(tool Tool, sourceDir string) ([]Issue, error
129134
func (suite *ToolTestSuite) TestStartTool() {
130135
r, w, oldStdout := setStdoutToBuffer()
131136

132-
defer func() {
133-
logrus.SetOutput(os.Stderr)
134-
}()
135-
136137
impl := ToolImplementationTest{}
137-
startToolImplementation(impl, "./")
138+
startToolImplementation(impl, suite.runConfig)
138139
issue := testIssue()
139140

140141
out, _ := readStdout(r, w)

0 commit comments

Comments
 (0)