-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
98 lines (85 loc) · 2.44 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package main
import (
"flag"
"fmt"
"io/ioutil"
"os"
"path"
"reflect"
)
func main() {
verbose := flag.Bool("v", false, "print verbose output")
flag.Parse()
success, err := runTest(*verbose)
if err != nil {
if *verbose {
fmt.Println(err)
}
os.Exit(1)
}
if !success {
if *verbose {
fmt.Println("Failed: Unicode file names were normalized")
}
os.Exit(1)
}
if *verbose {
fmt.Println("Success: Unicode file names were not normalized")
}
}
func runTest(verbose bool) (bool, error) {
tmpDir, err := ioutil.TempDir(".", "")
if err != nil {
return false, fmt.Errorf("failed to create temporary directory: %v", err)
}
defer func() {
if err := os.RemoveAll(tmpDir); err != nil {
fmt.Printf("failed to remove %s in defer: %v\n", tmpDir, err)
}
}()
// ö.txt
composed := string('\u00F6') + ".txt"
if err := ioutil.WriteFile(path.Join(tmpDir, composed), []byte("composed"), 0755); err != nil {
return false, fmt.Errorf("failed to write composed: %v", err)
}
// o¨.txt
decomposed := string('\u006F') + string('\u0308') + ".txt"
if err := ioutil.WriteFile(path.Join(tmpDir, decomposed), []byte("decomposed"), 0755); err != nil {
return false, fmt.Errorf("failed to write decomposed: %v", err)
}
files, err := ioutil.ReadDir(tmpDir)
if err != nil {
return false, fmt.Errorf("failed to list entries in directory %s: %v", tmpDir, err)
}
fileNames := make([]string, len(files))
for i := range files {
fileNames[i] = files[i].Name()
}
if verbose {
fmt.Printf("Number of files:\n")
fmt.Printf("\tExpected: %d\n", 2)
fmt.Printf("\tGot: %d\n", len(files))
fmt.Printf("Files:\n")
fmt.Printf("\tExpected: %v\n", []string{decomposed, composed})
fmt.Printf("\tGot: %v\n", fileNames)
}
composedBytes, err := ioutil.ReadFile(path.Join(tmpDir, composed))
if err != nil {
return false, fmt.Errorf("failed to read composed: %v", err)
}
if verbose {
fmt.Printf("Content of %s (\\u00F6.txt):\n", composed)
fmt.Printf("\tExpected: %s\n", "composed")
fmt.Printf("\tGot: %s\n", string(composedBytes))
}
decomposedBytes, err := ioutil.ReadFile(path.Join(tmpDir, decomposed))
if err != nil {
return false, fmt.Errorf("failed to read decomposed: %v", err)
}
if verbose {
fmt.Printf("Content of %s (\\u006F\\u0308.txt):\n", decomposed)
fmt.Printf("\tExpected: %s\n", "decomposed")
fmt.Printf("\tGot: %s\n", string(decomposedBytes))
}
return !reflect.DeepEqual(composedBytes, decomposedBytes), nil
}