v2 / vlib / veb / assets / assets_test.v
191 lines · 154 sloc · 5.33 KB · 8e35f4d9848f7ad35d857a187dddbfd2eca5e19d
Raw
1import veb.assets
2import os
3
4const base_cache_dir = os.join_path(os.vtmp_dir(), 'veb_assets_test_cache')
5
6fn testsuite_begin() {
7 os.mkdir_all(base_cache_dir) or {}
8}
9
10fn testsuite_end() {
11 os.rmdir_all(base_cache_dir) or {}
12}
13
14// clean_cache_dir used before and after tests that write to a cache directory.
15// Because of parallel compilation and therefore test running,
16// unique cache dirs are needed per test function.
17fn clean_cache_dir(dir string) {
18 os.rmdir_all(dir) or {}
19}
20
21fn cache_dir(test_name string) string {
22 return os.join_path(base_cache_dir, test_name)
23}
24
25fn get_test_file_path(file string) string {
26 path := os.join_path(base_cache_dir, file)
27 os.rm(path) or {}
28 os.write_file(path, get_test_file_contents(file)) or { panic(err) }
29 return path
30}
31
32fn get_test_file_contents(file string) string {
33 contents := match file {
34 'test1.js' { '{"one": 1}\n' }
35 'test2.js' { '{"two": 2}\n' }
36 'test1.css' { '.one {\n\tcolor: #336699;\n}\n' }
37 'test2.css' { '.two {\n\tcolor: #996633;\n}\n' }
38 else { 'wibble\n' }
39 }
40
41 return contents
42}
43
44fn test_add() {
45 mut am := assets.AssetManager{}
46
47 mut errored := false
48 am.add(.css, 'test.css', 'test.css') or { errored = true }
49 assert errored == true, 'am.add should error'
50
51 errored = false
52 am.add(.css, get_test_file_path('test1.css'), 'included.css') or {
53 eprintln(err)
54 errored = true
55 }
56 assert errored == false, 'am.add should not error'
57
58 css_assets := am.get_assets(.css)
59 assert css_assets.len == 1
60 assert css_assets[0].file_path == get_test_file_path('test1.css')
61 assert css_assets[0].include_name == 'included.css'
62}
63
64fn test_add_minify_missing_cache_dir() {
65 mut am := assets.AssetManager{
66 minify: true
67 }
68 mut errored := false
69 am.add(.js, get_test_file_path('test1.css'), 'included.js') or {
70 assert err.msg() == 'cannot minify asset: cache directory is not valid'
71 errored = true
72 }
73
74 assert errored == true, 'am.add should return an error'
75}
76
77fn test_add_minified() {
78 mut am := assets.AssetManager{
79 minify: true
80 cache_dir: cache_dir('test_add_minified')
81 }
82 clean_cache_dir(am.cache_dir)
83
84 am.add(.js, get_test_file_path('test1.js'), 'included.js')!
85
86 js_assets := am.get_assets(.js)
87 assert js_assets.len == 1
88 assert js_assets[0].file_path.starts_with(am.cache_dir) == true
89}
90
91fn test_combine() {
92 mut am := assets.AssetManager{
93 cache_dir: cache_dir('test_combine')
94 }
95 clean_cache_dir(am.cache_dir)
96
97 am.add(.css, get_test_file_path('test1.css'), 'test1.css')!
98 am.add(.css, get_test_file_path('test2.css'), 'test2.css')!
99
100 combined_path := am.combine(.css)!
101 combined := os.read_file(combined_path)!
102
103 expected := get_test_file_contents('test1.css') + '\n' + get_test_file_contents('test2.css') +
104 '\n'
105 assert combined == expected
106}
107
108fn test_combine_minified() {
109 // minify test is simple for now, because assets are not properly minified yet
110 mut am := assets.AssetManager{
111 cache_dir: cache_dir('test_combine_minified')
112 minify: true
113 }
114 clean_cache_dir(am.cache_dir)
115
116 am.add(.css, get_test_file_path('test1.css'), 'test1.css')!
117 am.add(.css, get_test_file_path('test2.css'), 'test2.css')!
118
119 combined_path := am.combine(.css)!
120 combined := os.read_file(combined_path)!
121
122 // minified version should be 2 lines + one extra newline
123 assert combined.split('\n').len == 3
124}
125
126fn test_minify_cache_last_modified() {
127 mut am := assets.AssetManager{
128 minify: true
129 cache_dir: cache_dir('test_cache_last_modified')
130 }
131 clean_cache_dir(am.cache_dir)
132
133 // first we write the file and add it
134 am.add(.js, get_test_file_path('test1.js'), 'included.js')!
135 mut js_assets := am.get_assets(.js)
136 assert js_assets.len == 1
137 old_cached_path := js_assets[0].file_path
138
139 // then we only add the file, the file is not modified so the "last modified is the same".
140 // we expect that the asset manager doesn't cache a minified file if it hasn't been changed
141 // the last time it was added
142 am.add(.js, os.join_path(base_cache_dir, 'test1.js'), 'included.js')!
143
144 js_assets = am.get_assets(.js)
145 // check if the file isn't added twice
146 assert js_assets.len == 1
147 // if the file path was not modified, veb.assets didn't overwrite the file
148 assert js_assets[0].file_path == old_cached_path
149}
150
151fn test_cleanup_cache() {
152 mut am := assets.AssetManager{
153 minify: true
154 cache_dir: cache_dir('test_cleanup_cache')
155 }
156 clean_cache_dir(am.cache_dir)
157 // manually make the cache dir
158 os.mkdir_all(am.cache_dir) or {}
159
160 // write a file to the cache dir isn't added to the asset manager to represent
161 // a previously cached file
162 path1 := os.join_path(am.cache_dir, 'test1.css')
163 os.write_file(path1, 'h1 { color: red; }')!
164 assert os.exists(path1) == true
165
166 // add a file to the asset manager and write it
167 am.add(.css, get_test_file_path('test2.css'), 'test2.css')!
168 css_assets := am.get_assets(.css)
169 // get the cached path
170 assert css_assets.len == 1
171 path2 := css_assets[0].file_path
172 assert os.exists(path2) == true
173
174 am.cleanup_cache()!
175
176 // the first asset wasn't added to the asset manager, so it should not exist
177 assert os.exists(path1) == false
178 assert os.exists(path2) == true
179}
180
181fn test_include() {
182 mut am := assets.AssetManager{}
183
184 css_path := get_test_file_path('test1.css')
185 js_path := get_test_file_path('test1.js')
186 am.add(.css, css_path, 'other.css')!
187 am.add(.js, js_path, 'js/test.js')!
188
189 assert am.include(.css, 'other.css') == '<link rel="stylesheet" href="${css_path}">'
190 assert am.include(.js, 'js/test.js') == '<script src="${js_path}"></script>'
191}
192