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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
|
package odinfmt_testing
import "core:testing"
import "core:os"
import "core:path/filepath"
import "core:strings"
import "core:text/scanner"
import "core:fmt"
import "shared:odin/format"
format_file :: proc(
filepath: string,
allocator := context.allocator,
) -> (
string,
bool,
) {
style := format.default_style
style.character_width = 80
style.newline_style = .LF //We want to make sure it works on linux and windows.
if data, ok := os.read_entire_file(filepath, allocator); ok {
return format.format(
filepath,
string(data),
style,
{.Optional_Semicolons},
allocator,
)
} else {
return "", false
}
}
snapshot_directory :: proc(directory: string) -> bool {
matches, err := filepath.glob(fmt.tprintf("%v/*", directory))
if err != .None {
fmt.eprintf("Error in globbing directory: %v", directory)
}
for match in matches {
if strings.contains(match, ".odin") {
snapshot_file(match) or_return
}
}
for match in matches {
if !strings.contains(match, ".snapshots") {
if os.is_dir(match) {
snapshot_directory(match)
}
}
}
return true
}
snapshot_file :: proc(path: string) -> bool {
fmt.printf("Testing snapshot %v", path)
snapshot_path := filepath.join(
elems = {
filepath.dir(path, context.temp_allocator),
"/.snapshots",
filepath.base(path),
},
allocator = context.temp_allocator,
)
formatted, ok := format_file(path, context.temp_allocator)
if !ok {
fmt.eprintf("Format failed on file %v", path)
return false
}
if os.exists(snapshot_path) {
if snapshot_data, ok := os.read_entire_file(
snapshot_path,
context.temp_allocator,
); ok {
snapshot_scanner := scanner.Scanner{}
scanner.init(&snapshot_scanner, string(snapshot_data))
formatted_scanner := scanner.Scanner{}
scanner.init(&formatted_scanner, string(formatted))
for {
s_ch := scanner.next(&snapshot_scanner)
f_ch := scanner.next(&formatted_scanner)
if s_ch == scanner.EOF && f_ch == scanner.EOF {
break
}
if s_ch == '\r' {
if scanner.peek(&snapshot_scanner) == '\n' {
s_ch = scanner.next(&snapshot_scanner)
}
}
if f_ch == '\r' {
if scanner.peek(&formatted_scanner) == '\n' {
f_ch = scanner.next(&formatted_scanner)
}
}
if s_ch != f_ch {
fmt.eprintf(
"\nFormatted file was different from snapshot file: %v",
snapshot_path,
)
os.write_entire_file(
fmt.tprintf("%v_failed", snapshot_path),
transmute([]u8)formatted,
)
return false
}
}
os.remove(fmt.tprintf("%v_failed", snapshot_path))
} else {
fmt.eprintf("Failed to read snapshot file %v", snapshot_path)
return false
}
} else {
os.make_directory(filepath.dir(snapshot_path, context.temp_allocator))
ok = os.write_entire_file(snapshot_path, transmute([]byte)formatted)
if !ok {
fmt.eprintf("Failed to write snapshot file %v", snapshot_path)
return false
}
}
fmt.print(" - SUCCESS \n")
return true
}
|