Skip to content

Commit e9e9bb9

Browse files
authored
#69: Use alternative golangci-lint (#70)
* #69: Use alternative golangci-lint * #69: Fix lint issues for tests in os.Create/os.Remove functions
1 parent e746c86 commit e9e9bb9

File tree

3 files changed

+55
-26
lines changed

3 files changed

+55
-26
lines changed

.github/workflows/go.yml

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,15 @@ jobs:
1515
go-version: [1.17.x]
1616
runs-on: ubuntu-latest
1717
steps:
18-
- uses: actions/checkout@v2
19-
20-
- name: golangci-lint
21-
uses: reviewdog/action-golangci-lint@v2
18+
- name: Set up Go
19+
uses: actions/setup-go@v3
20+
with:
21+
go-version: ${{ matrix.go-version }}
22+
- uses: actions/checkout@v3
23+
- name: Run golangci-lint
24+
uses: golangci/golangci-lint-action@v3
25+
with:
26+
version: latest
2227
test:
2328
strategy:
2429
matrix:

cmd/root_test.go

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,15 @@ func Test_validate(t *testing.T) {
4242
name string
4343
args args
4444
wantErr bool
45-
before func()
46-
after func()
45+
before func(t *testing.T)
46+
after func(t *testing.T)
4747
}{
4848
{
4949
name: "Wrong output format",
5050
args: args{config: formatter.Config{OutputFormat: formatter.OutputFormat("test")}},
5151
wantErr: true,
52-
before: func() {},
53-
after: func() {},
52+
before: func(t *testing.T) {},
53+
after: func(t *testing.T) {},
5454
},
5555
{
5656
name: "Missing input file",
@@ -60,8 +60,8 @@ func Test_validate(t *testing.T) {
6060
},
6161
},
6262
wantErr: true,
63-
before: func() {},
64-
after: func() {},
63+
before: func(t *testing.T) {},
64+
after: func(t *testing.T) {},
6565
},
6666
{
6767
name: "Successful validation",
@@ -72,18 +72,26 @@ func Test_validate(t *testing.T) {
7272
},
7373
},
7474
wantErr: false,
75-
before: func() {
76-
os.Create(path.Join(os.TempDir(), "formatter_cmd_valid_2"))
75+
before: func(t *testing.T) {
76+
path := path.Join(os.TempDir(), "formatter_cmd_valid_2")
77+
_, err := os.Create(path)
78+
if err != nil {
79+
t.Errorf("could not create temporary file: %s", path)
80+
}
7781
},
78-
after: func() {
79-
os.Remove(path.Join(os.TempDir(), "formatter_cmd_valid_2"))
82+
after: func(t *testing.T) {
83+
path := path.Join(os.TempDir(), "formatter_cmd_valid_2")
84+
err := os.Remove(path)
85+
if err != nil {
86+
t.Errorf("could not remove temporary file: %s", path)
87+
}
8088
},
8189
},
8290
}
8391
for _, tt := range tests {
8492
t.Run(tt.name, func(t *testing.T) {
85-
tt.before()
86-
defer tt.after()
93+
tt.before(t)
94+
defer tt.after(t)
8795
if err := validate(tt.args.config); (err != nil) != tt.wantErr {
8896
t.Errorf("validate() error = %v, wantErr %v", err, tt.wantErr)
8997
}
@@ -145,14 +153,26 @@ func Test_run(t *testing.T) {
145153
cmd *cobra.Command
146154
args []string
147155
}
148-
before := func(file string, testWorkflow formatter.Workflow, testConfig formatter.Config) {
149-
os.Create(file)
156+
before := func(file string, testWorkflow formatter.Workflow, testConfig formatter.Config, t *testing.T) {
157+
var err error
158+
if len(file) != 0 {
159+
_, err = os.Create(file)
160+
}
161+
if err != nil {
162+
t.Errorf("could not create temporary file: %s", file)
163+
}
150164
workflow = testWorkflow
151165
config = testConfig
152166
config.InputFile = formatter.InputFile(file)
153167
}
154-
after := func(file string) {
155-
os.Remove(file)
168+
after := func(file string, t *testing.T) {
169+
var err error
170+
if len(file) != 0 {
171+
err = os.Remove(file)
172+
}
173+
if err != nil {
174+
t.Errorf("could not remove temporary file: %s", file)
175+
}
156176
workflow = nil
157177
config = formatter.Config{}
158178
}
@@ -212,8 +232,8 @@ func Test_run(t *testing.T) {
212232
for _, tt := range tests {
213233
t.Run(tt.name, func(t *testing.T) {
214234
if tt.runBefore {
215-
before(tt.input, tt.workflow, tt.config)
216-
defer after(tt.input)
235+
before(tt.input, tt.workflow, tt.config, t)
236+
defer after(tt.input, t)
217237
}
218238
if err := run(tt.args.cmd, tt.args.args); (err != nil) != tt.wantErr {
219239
t.Errorf("run() error = %v, wantErr %v", err, tt.wantErr)

formatter/workflow_test.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ func TestMainWorkflow_Execute(t *testing.T) {
100100
wantErr bool
101101
fileName string
102102
fileContent string
103-
before func(file string)
103+
before func(file string, t *testing.T)
104104
}{
105105
{
106106
name: "Cannot open OutputFile for a write",
@@ -119,9 +119,13 @@ func TestMainWorkflow_Execute(t *testing.T) {
119119
wantErr: true,
120120
fileName: "main_workflow_Execute_2_test",
121121
fileContent: "",
122-
before: func(file string) {
122+
before: func(file string, t *testing.T) {
123123
// Creating output file
124-
os.Create(path.Join(os.TempDir(), file+"_output"))
124+
path := path.Join(os.TempDir(), file+"_output")
125+
_, err := os.Create(path)
126+
if err != nil {
127+
t.Errorf("failed to create temporary output file: %s", path)
128+
}
125129
},
126130
},
127131
{
@@ -159,7 +163,7 @@ func TestMainWorkflow_Execute(t *testing.T) {
159163
for _, tt := range tests {
160164
t.Run(tt.name, func(t *testing.T) {
161165
if tt.before != nil {
162-
tt.before(tt.fileName)
166+
tt.before(tt.fileName, t)
163167
}
164168
if tt.fileName != "" {
165169
name := path.Join(os.TempDir(), tt.fileName)

0 commit comments

Comments
 (0)