@@ -2,10 +2,8 @@ package misconf
2
2
3
3
import (
4
4
"context"
5
- "fmt"
6
5
"os"
7
6
"path/filepath"
8
- "runtime"
9
7
"testing"
10
8
11
9
"github.com/stretchr/testify/assert"
@@ -63,111 +61,18 @@ func TestScanner_Scan(t *testing.T) {
63
61
}
64
62
}
65
63
66
- func Test_FindingFSTarget (t * testing.T ) {
67
- tests := []struct {
68
- input []string
69
- wantTarget string
70
- wantPaths []string
71
- wantErr bool
72
- }{
73
- {
74
- input : nil ,
75
- wantErr : true ,
76
- },
77
- {
78
- input : []string {string (os .PathSeparator )},
79
- wantTarget : string (os .PathSeparator ),
80
- wantPaths : []string {"." },
81
- },
82
- {
83
- input : []string {filepath .Join (string (os .PathSeparator ), "home" , "user" )},
84
- wantTarget : filepath .Join (string (os .PathSeparator ), "home" , "user" ),
85
- wantPaths : []string {"." },
86
- },
87
- {
88
- input : []string {
89
- filepath .Join (string (os .PathSeparator ), "home" , "user" ),
90
- filepath .Join (string (os .PathSeparator ), "home" , "user" , "something" ),
91
- },
92
- wantTarget : filepath .Join (string (os .PathSeparator ), "home" , "user" ),
93
- wantPaths : []string {"." , "something" },
94
- },
95
- {
96
- input : []string {
97
- filepath .Join (string (os .PathSeparator ), "home" , "user" ),
98
- filepath .Join (string (os .PathSeparator ), "home" , "user" , "something" , "else" ),
99
- },
100
- wantTarget : filepath .Join (string (os .PathSeparator ), "home" , "user" ),
101
- wantPaths : []string {"." , "something/else" },
102
- },
103
- {
104
- input : []string {
105
- filepath .Join (string (os .PathSeparator ), "home" , "user" ),
106
- filepath .Join (string (os .PathSeparator ), "home" , "user2" , "something" , "else" ),
107
- },
108
- wantTarget : filepath .Join (string (os .PathSeparator ), "home" ),
109
- wantPaths : []string {"user" , "user2/something/else" },
110
- },
111
- {
112
- input : []string {
113
- filepath .Join (string (os .PathSeparator ), "foo" ),
114
- filepath .Join (string (os .PathSeparator ), "bar" ),
115
- },
116
- wantTarget : string (os .PathSeparator ),
117
- wantPaths : []string {"foo" , "bar" },
118
- },
119
- {
120
- input : []string {string (os .PathSeparator ), filepath .Join (string (os .PathSeparator ), "bar" )},
121
- wantTarget : string (os .PathSeparator ),
122
- wantPaths : []string {"." , "bar" },
123
- },
124
- }
125
-
126
- for _ , test := range tests {
127
- t .Run (fmt .Sprintf ("%#v" , test .input ), func (t * testing.T ) {
128
- if runtime .GOOS == "windows" {
129
- wantTarget , err := filepath .Abs (test .wantTarget )
130
- require .NoError (t , err )
131
- test .wantTarget = filepath .Clean (wantTarget )
132
- }
133
-
134
- target , paths , err := findFSTarget (test .input )
135
- if test .wantErr {
136
- require .Error (t , err )
137
- } else {
138
- assert .Equal (t , test .wantTarget , target )
139
- assert .Equal (t , test .wantPaths , paths )
140
- }
141
- })
142
- }
143
- }
144
-
145
64
func Test_createPolicyFS (t * testing.T ) {
146
- t .Run ("inside cwd" , func (t * testing.T ) {
147
- cwd , err := os .Getwd ()
148
- require .NoError (t , err )
149
- require .NoError (t , os .MkdirAll (filepath .Join (cwd , "testdir" ), 0750 ))
150
- require .NoError (t , os .MkdirAll (filepath .Join (cwd , ".testdir" ), 0750 ))
151
- defer func () {
152
- os .RemoveAll (filepath .Join (cwd , "testdir" ))
153
- os .RemoveAll (filepath .Join (cwd , ".testdir" ))
154
- }()
155
-
156
- _ , got1 , err := createPolicyFS ([]string {"testdir" })
157
- require .NoError (t , err )
158
-
159
- _ , got2 , err := createPolicyFS ([]string {".testdir" })
160
- require .NoError (t , err )
161
-
162
- assert .NotEqual (t , got1 , got2 , "testdir and .testdir are different dirs and should not be equal" )
163
- })
164
-
165
- t .Run ("outside cwd" , func (t * testing.T ) {
65
+ t .Run ("outside pwd" , func (t * testing.T ) {
166
66
tmpDir := t .TempDir ()
167
67
require .NoError (t , os .MkdirAll (filepath .Join (tmpDir , "subdir/testdir" ), 0750 ))
168
68
f , got , err := createPolicyFS ([]string {filepath .Join (tmpDir , "subdir/testdir" )})
169
69
require .NoError (t , err )
170
70
assert .Equal (t , []string {"." }, got )
171
- assert .Contains (t , f , "testdir" )
71
+
72
+ d , err := f .Open (tmpDir )
73
+ require .NoError (t , err )
74
+ stat , err := d .Stat ()
75
+ require .NoError (t , err )
76
+ assert .True (t , stat .IsDir ())
172
77
})
173
78
}
0 commit comments