Skip to content

Commit aa5cd21

Browse files
committed
add tests
1 parent c85c102 commit aa5cd21

File tree

5 files changed

+135
-5
lines changed

5 files changed

+135
-5
lines changed

hw08_envdir_tool/env_reader_test.go

Lines changed: 71 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,76 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"os"
6+
"testing"
7+
)
48

59
func TestReadDir(t *testing.T) {
6-
// Place your code here
10+
testCases := []struct {
11+
name string
12+
dir string
13+
wantEnt Environment
14+
err error
15+
}{
16+
{
17+
"valid env",
18+
"./testdata/env",
19+
Environment{
20+
"BAR": EnvValue{Value: "bar", NeedRemove: false},
21+
"EMPTY": EnvValue{Value: "", NeedRemove: false},
22+
"FOO": EnvValue{Value: " foo\nwith new line", NeedRemove: false},
23+
"HELLO": EnvValue{Value: "\"hello\"", NeedRemove: false},
24+
"UNSET": EnvValue{Value: "", NeedRemove: true},
25+
},
26+
nil,
27+
},
28+
{
29+
"non-existent dir",
30+
"./testdata/efoewf",
31+
nil,
32+
os.ErrNotExist,
33+
},
34+
}
35+
36+
for _, tc := range testCases {
37+
t.Run(tc.name, func(t *testing.T) {
38+
actual, err := ReadDir(tc.dir)
39+
if tc.err != nil {
40+
assert.ErrorIs(t, err, tc.err)
41+
}
42+
assert.Equal(t, tc.wantEnt, actual)
43+
})
44+
}
45+
}
46+
47+
func TestEnvValue_NeedSetToEnv(t *testing.T) {
48+
testCases := []struct {
49+
name string
50+
value EnvValue
51+
want bool
52+
}{
53+
{
54+
"valid to set",
55+
EnvValue{Value: "", NeedRemove: false},
56+
true,
57+
},
58+
{
59+
"valid to set non empty",
60+
EnvValue{Value: "efefe", NeedRemove: true},
61+
true,
62+
},
63+
{
64+
"valid to unset",
65+
EnvValue{Value: "", NeedRemove: true},
66+
false,
67+
},
68+
}
69+
for _, tc := range testCases {
70+
t.Run(tc.name, func(t *testing.T) {
71+
if got := tc.value.NeedSetToEnv(); got != tc.want {
72+
t.Fail()
73+
}
74+
})
75+
}
776
}

hw08_envdir_tool/executor.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ func RunCmd(cmd []string, env Environment) (returnCode int) {
2323

2424
func updateEnv(env Environment) error {
2525
for k, v := range env {
26-
// Независимо от назначения элемента из списка его нужно удалить. Часть элементов после удаления нужно присвоить заново.
26+
// Независимо от назначения элемента из списка его нужно удалить.
27+
// Часть элементов после удаления нужно присвоить заново.
2728
// Удаление происходит за 1 операцию, без предварительной проверки.
2829
if err := os.Unsetenv(k); err != nil {
2930
return err

hw08_envdir_tool/executor_test.go

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,46 @@
11
package main
22

3-
import "testing"
3+
import (
4+
"github.com/stretchr/testify/assert"
5+
"testing"
6+
)
47

58
func TestRunCmd(t *testing.T) {
6-
// Place your code here
9+
testCases := []struct {
10+
name string
11+
cmd []string
12+
env Environment
13+
code int
14+
}{
15+
{
16+
"valid echo script",
17+
[]string{"./testdata/echo.sh"},
18+
nil,
19+
ReturnCodeOk,
20+
},
21+
{
22+
"non-existent echo script",
23+
[]string{"./testdata/oedeokd"},
24+
nil,
25+
ReturnCodeErr,
26+
},
27+
{
28+
"valid test script",
29+
[]string{"./test.sh"},
30+
Environment{
31+
"BAR": EnvValue{Value: "bar", NeedRemove: false},
32+
"EMPTY": EnvValue{Value: "", NeedRemove: false},
33+
"FOO": EnvValue{Value: " foo\nwith new line", NeedRemove: false},
34+
"HELLO": EnvValue{Value: "\"hello\"", NeedRemove: false},
35+
"UNSET": EnvValue{Value: "", NeedRemove: true},
36+
},
37+
ReturnCodeOk,
38+
},
39+
}
40+
for _, tc := range testCases {
41+
t.Run(tc.name, func(t *testing.T) {
42+
code := RunCmd(tc.cmd, tc.env)
43+
assert.Equal(t, tc.code, code)
44+
})
45+
}
746
}

hw08_envdir_tool/go.mod

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
11
module github.com/kpechenenko/hw08_envdir_tool
22

33
go 1.19
4+
5+
require github.com/stretchr/testify v1.7.0
6+
7+
require (
8+
github.com/davecgh/go-spew v1.1.1 // indirect
9+
github.com/pmezard/go-difflib v1.0.0 // indirect
10+
gopkg.in/yaml.v3 v3.0.1 // indirect
11+
)

hw08_envdir_tool/go.sum

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
2+
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
3+
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
4+
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
5+
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
6+
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
7+
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
8+
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
9+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
10+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
11+
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
12+
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
13+
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

0 commit comments

Comments
 (0)