Skip to content

Commit d69d260

Browse files
committed
add tests
1 parent 55a8d38 commit d69d260

File tree

1 file changed

+60
-2
lines changed

1 file changed

+60
-2
lines changed

crates/oxide/tests/scan_dir.rs

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
11
#[cfg(test)]
22
mod scan_dir {
3+
use serial_test::serial;
34
use std::fs;
45
use std::process::Command;
56

67
use tailwindcss_oxide::*;
78
use tempfile::tempdir;
89

9-
fn scan(paths_with_content: &[(&str, Option<&str>)]) -> (Vec<String>, Vec<String>) {
10+
fn scan_with_globs(
11+
paths_with_content: &[(&str, Option<&str>)],
12+
globs: Vec<&str>,
13+
) -> (Vec<String>, Vec<String>) {
14+
// Ensure that every test truly runs in isolation without any cache
15+
clear_cache();
16+
1017
// Create a temporary working directory
1118
let dir = tempdir().unwrap().into_path();
1219

@@ -30,7 +37,10 @@ mod scan_dir {
3037
let base = format!("{}", dir.display());
3138

3239
// Resolve all content paths for the (temporary) current working directory
33-
let result = scan_dir(ScanOptions { base: base.clone() });
40+
let result = scan_dir(ScanOptions {
41+
base: base.clone(),
42+
content_paths: globs.iter().map(|x| x.to_string()).collect(),
43+
});
3444

3545
let mut paths: Vec<_> = result
3646
.files
@@ -57,11 +67,16 @@ mod scan_dir {
5767
(paths, result.candidates)
5868
}
5969

70+
fn scan(paths_with_content: &[(&str, Option<&str>)]) -> (Vec<String>, Vec<String>) {
71+
scan_with_globs(paths_with_content, vec![])
72+
}
73+
6074
fn test(paths_with_content: &[(&str, Option<&str>)]) -> Vec<String> {
6175
scan(paths_with_content).0
6276
}
6377

6478
#[test]
79+
#[serial]
6580
fn it_should_work_with_a_set_of_root_files() {
6681
let globs = test(&[
6782
("index.html", None),
@@ -73,6 +88,7 @@ mod scan_dir {
7388
}
7489

7590
#[test]
91+
#[serial]
7692
fn it_should_work_with_a_set_of_root_files_and_ignore_ignored_files() {
7793
let globs = test(&[
7894
(".gitignore", Some("b.html")),
@@ -85,6 +101,7 @@ mod scan_dir {
85101
}
86102

87103
#[test]
104+
#[serial]
88105
fn it_should_list_all_files_in_the_public_folder_explicitly() {
89106
let globs = test(&[
90107
("index.html", None),
@@ -104,6 +121,7 @@ mod scan_dir {
104121
}
105122

106123
#[test]
124+
#[serial]
107125
fn it_should_list_nested_folders_explicitly_in_the_public_folder() {
108126
let globs = test(&[
109127
("index.html", None),
@@ -133,6 +151,7 @@ mod scan_dir {
133151
}
134152

135153
#[test]
154+
#[serial]
136155
fn it_should_list_all_files_in_the_public_folder_explicitly_except_ignored_files() {
137156
let globs = test(&[
138157
(".gitignore", Some("public/b.html\na.html")),
@@ -145,6 +164,7 @@ mod scan_dir {
145164
}
146165

147166
#[test]
167+
#[serial]
148168
fn it_should_use_a_glob_for_top_level_folders() {
149169
let globs = test(&[
150170
("index.html", None),
@@ -162,6 +182,7 @@ mod scan_dir {
162182
}
163183

164184
#[test]
185+
#[serial]
165186
fn it_should_ignore_binary_files() {
166187
let globs = test(&[
167188
("index.html", None),
@@ -173,6 +194,7 @@ mod scan_dir {
173194
}
174195

175196
#[test]
197+
#[serial]
176198
fn it_should_ignore_known_extensions() {
177199
let globs = test(&[
178200
("index.html", None),
@@ -184,6 +206,7 @@ mod scan_dir {
184206
}
185207

186208
#[test]
209+
#[serial]
187210
fn it_should_ignore_known_files() {
188211
let globs = test(&[
189212
("index.html", None),
@@ -194,6 +217,7 @@ mod scan_dir {
194217
}
195218

196219
#[test]
220+
#[serial]
197221
fn it_should_ignore_and_expand_nested_ignored_folders() {
198222
let globs = test(&[
199223
// Explicitly listed root files
@@ -280,6 +304,7 @@ mod scan_dir {
280304
}
281305

282306
#[test]
307+
#[serial]
283308
fn it_should_scan_for_utilities() {
284309
let mut ignores = String::new();
285310
ignores.push_str("# md:font-bold\n");
@@ -304,4 +329,37 @@ mod scan_dir {
304329
vec!["condition", "div", "font-bold", "md:flex", "px-4"]
305330
);
306331
}
332+
333+
#[test]
334+
#[serial]
335+
fn it_should_scan_content_paths() {
336+
let candidates = scan_with_globs(
337+
&[
338+
// We know that `.styl` extensions are ignored, so they are not covered by auto content
339+
// detection.
340+
("foo.styl", Some("content-['foo.styl']")),
341+
],
342+
vec!["*.styl"],
343+
)
344+
.1;
345+
346+
assert_eq!(candidates, vec!["content-['foo.styl']"]);
347+
}
348+
349+
#[test]
350+
#[serial]
351+
fn it_should_scan_content_paths_even_when_they_are_git_ignored() {
352+
let candidates = scan_with_globs(
353+
&[
354+
(".gitignore", Some("foo.styl")),
355+
// We know that `.styl` extensions are ignored, so they are not covered by auto content
356+
// detection.
357+
("foo.styl", Some("content-['foo.styl']")),
358+
],
359+
vec!["*.styl"],
360+
)
361+
.1;
362+
363+
assert_eq!(candidates, vec!["content-['foo.styl']"]);
364+
}
307365
}

0 commit comments

Comments
 (0)