File tree Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Expand file tree Collapse file tree 2 files changed +43
-3
lines changed Original file line number Diff line number Diff line change @@ -3,8 +3,8 @@ package scanner
3
3
import (
4
4
"fmt"
5
5
"os"
6
+ "regexp"
6
7
"runtime"
7
- "strings"
8
8
9
9
"golang.org/x/xerrors"
10
10
"gopkg.in/yaml.v3"
@@ -18,8 +18,8 @@ func createTempFile(artifact *artifacts.Artifact) (string, error) {
18
18
filename := fmt .Sprintf ("%s-%s-%s-*.yaml" , artifact .Namespace , artifact .Kind , artifact .Name )
19
19
20
20
if runtime .GOOS == "windows" {
21
- //sanitize file name to removed unsupported char on windows os
22
- filename = strings . ReplaceAll (filename , ":" , "-" )
21
+ //removes characters not permitted in file/directory names on Windows
22
+ filename = filenameWindowsFriendly (filename )
23
23
}
24
24
file , err := os .CreateTemp ("" , filename )
25
25
if err != nil {
@@ -44,3 +44,9 @@ func removeFile(filename string) {
44
44
log .Logger .Errorf ("failed to remove temp file %s: %s:" , filename , err )
45
45
}
46
46
}
47
+
48
+ var r , _ = regexp .Compile ("\\ \\ |/|:|\\ *|\\ ?|<|>" )
49
+
50
+ func filenameWindowsFriendly (name string ) string {
51
+ return r .ReplaceAllString (name , "_" )
52
+ }
Original file line number Diff line number Diff line change
1
+ package scanner
2
+
3
+ import (
4
+ "testing"
5
+
6
+ "github.com/stretchr/testify/assert"
7
+ )
8
+
9
+ func Test_FilenameWindowsFriendly (t * testing.T ) {
10
+
11
+ tests := []struct {
12
+ name string
13
+ fileName string
14
+ want string
15
+ }{
16
+ {
17
+ name : "name with invalid char - colon" ,
18
+ fileName : `kube-system-Role-system:controller:bootstrap-signer-2934213283.yaml` ,
19
+ want : `kube-system-Role-system_controller_bootstrap-signer-2934213283.yaml` ,
20
+ },
21
+ {
22
+ name : "name with no invalid chars" ,
23
+ fileName : `kube-system-Role-system-controller-bootstrap-signer-2934213283.yaml` ,
24
+ want : `kube-system-Role-system-controller-bootstrap-signer-2934213283.yaml` ,
25
+ },
26
+ }
27
+
28
+ for _ , test := range tests {
29
+ t .Run (test .name , func (t * testing.T ) {
30
+ got := filenameWindowsFriendly (test .fileName )
31
+ assert .Equal (t , test .want , got )
32
+ })
33
+ }
34
+ }
You can’t perform that action at this time.
0 commit comments