#[cfg(test)]
mod scanner {
use pretty_assertions::assert_eq;
use std::path::{Path, PathBuf};
use std::process::Command;
use std::thread::sleep;
use std::time::Duration;
use std::{fs, path};
use tailwindcss_oxide::*;
use tempfile::tempdir;
fn symlink<P: AsRef<Path>, Q: AsRef<Path>>(original: P, link: Q) -> std::io::Result<()> {
#[cfg(not(windows))]
let result = std::os::unix::fs::symlink(original, link);
#[cfg(windows)]
let result = std::os::windows::fs::symlink_dir(original, link);
result
}
fn public_source_entry_from_pattern(dir: PathBuf, pattern: &str) -> PublicSourceEntry {
let mut parts = pattern.split_whitespace();
let _ = parts.next().unwrap_or_default();
let not_or_pattern = parts.next().unwrap_or_default();
if not_or_pattern == "not" {
let pattern = parts.next().unwrap_or_default();
return PublicSourceEntry {
base: dir.to_string_lossy().into(),
pattern: pattern[1..pattern.len() - 1].to_string(),
negated: true,
};
}
PublicSourceEntry {
base: dir.to_string_lossy().into(),
pattern: not_or_pattern[1..not_or_pattern.len() - 1].to_string(),
negated: false,
}
}
struct ScanResult {
files: Vec<String>,
globs: Vec<String>,
normalized_sources: Vec<String>,
candidates: Vec<String>,
}
fn create_files_in(dir: &path::Path, paths: &[(&str, &str)]) {
for (path, contents) in paths {
let path = dir.join(path.replace('/', path::MAIN_SEPARATOR.to_string().as_str()));
let parent = path.parent().unwrap();
if !parent.exists() {
fs::create_dir_all(parent).unwrap();
}
fs::write(path, contents).unwrap()
}
}
fn scan_with_globs(
paths_with_content: &[(&str, &str)],
source_directives: Vec<&str>,
) -> ScanResult {
let dir = tempdir().unwrap().into_path();
let _ = Command::new("git").arg("init").current_dir(&dir).output();
self::create_files_in(&dir, paths_with_content);
let base = format!("{}", dir.display()).replace('\\', "/");
let sources: Vec<PublicSourceEntry> = source_directives
.iter()
.map(|str| public_source_entry_from_pattern(base.clone().into(), str))
.collect();
let mut scanner = Scanner::new(sources);
let candidates = scanner.scan();
let base_dir =
format!("{}{}", dunce::canonicalize(&base).unwrap().display(), "/").replace('\\', "/");
let mut files = scanner
.get_files()
.iter()
.map(|file| file.replace('\\', "/").replace(&base_dir, ""))
.collect::<Vec<_>>();
files.sort();
let mut globs = scanner
.get_globs()
.iter()
.map(|glob| {
if glob.pattern.starts_with('/') {
format!("{}{}", glob.base, glob.pattern)
} else {
format!("{}/{}", glob.base, glob.pattern)
}
})
.map(|file| file.replace('\\', "/").replace(&base_dir, ""))
.collect::<Vec<_>>();
globs.sort();
let mut normalized_sources = scanner
.get_normalized_sources()
.iter()
.map(|glob| {
if glob.pattern.starts_with('/') {
format!("{}{}", glob.base, glob.pattern)
} else {
format!("{}/{}", glob.base, glob.pattern)
}
})
.map(|file| file.replace('\\', "/").replace(&base_dir, ""))
.collect::<Vec<_>>();
normalized_sources.sort();
ScanResult {
files,
globs,
normalized_sources,
candidates,
}
}
fn scan(paths_with_content: &[(&str, &str)]) -> ScanResult {
scan_with_globs(paths_with_content, vec!["@source '**/*'"])
}
#[test]
fn it_should_work_with_a_set_of_root_files() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("a.html", ""),
("b.html", ""),
("c.html", ""),
]);
assert_eq!(files, vec!["a.html", "b.html", "c.html", "index.html"]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_work_with_a_set_of_root_files_and_ignore_ignored_files() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
(".gitignore", "b.html"),
("index.html", ""),
("a.html", ""),
("b.html", ""),
("c.html", ""),
]);
assert_eq!(files, vec!["a.html", "c.html", "index.html"]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_list_all_files_in_the_public_folder_explicitly() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("public/a.html", ""),
("public/b.html", ""),
("public/c.html", ""),
("public/nested/c.html", ""),
("public/deeply/nested/c.html", ""),
]);
assert_eq!(
files,
vec![
"index.html",
"public/a.html",
"public/b.html",
"public/c.html",
"public/deeply/nested/c.html",
"public/nested/c.html",
]
);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_list_nested_folders_explicitly_in_the_public_folder() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("public/a.html", ""),
("public/b.html", ""),
("public/c.html", ""),
("public/nested/a.html", ""),
("public/nested/b.html", ""),
("public/nested/c.html", ""),
("public/nested/again/a.html", ""),
("public/very/deeply/nested/a.html", ""),
]);
assert_eq!(
files,
vec![
"index.html",
"public/a.html",
"public/b.html",
"public/c.html",
"public/nested/a.html",
"public/nested/again/a.html",
"public/nested/b.html",
"public/nested/c.html",
"public/very/deeply/nested/a.html",
]
);
assert_eq!(globs, vec!["*",]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_list_all_files_in_the_public_folder_explicitly_except_ignored_files() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
(".gitignore", "public/b.html\na.html"),
("index.html", ""),
("public/a.html", ""),
("public/b.html", ""),
("public/c.html", ""),
]);
assert_eq!(files, vec!["index.html", "public/c.html",]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_use_a_glob_for_top_level_folders() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("src/a.html", ""),
("src/b.html", ""),
("src/c.html", ""),
]);
assert_eq!(
files,
vec!["index.html", "src/a.html", "src/b.html", "src/c.html"]
);
assert_eq!(globs, vec![
"*",
"src/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_ignore_binary_files() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("a.mp4", ""),
("b.png", ""),
("c.lock", ""),
]);
assert_eq!(files, vec!["index.html"]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_not_ignore_folders_that_end_with_a_binary_extension() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("some.pages/index.html", "content-['some.pages/index.html']"),
(".gitignore", "other.pages"),
(
"other.pages/index.html",
"content-['other.pages/index.html']",
),
]);
assert_eq!(files, vec!["some.pages/index.html"]);
assert_eq!(globs, vec!["*", "some.pages/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_ignore_known_extensions() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("a.css", ""),
("b.sass", ""),
("c.less", ""),
]);
assert_eq!(files, vec!["a.css", "index.html"]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_find_new_extensions() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[("src/index.my-extension", "")]);
assert_eq!(files, vec!["src/index.my-extension"]);
assert_eq!(globs, vec!["*", "src/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,my-extension,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_ignore_known_files() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("index.html", ""),
("package-lock.json", ""),
("yarn.lock", ""),
]);
assert_eq!(files, vec!["index.html"]);
assert_eq!(globs, vec!["*"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_ignore_and_expand_nested_ignored_folders() {
let ScanResult {
files,
globs,
normalized_sources,
..
} = scan(&[
("foo.html", ""),
("bar.html", ""),
("baz.html", ""),
("nested-a/foo.html", ""),
("nested-a/bar.html", ""),
("nested-a/baz.html", ""),
("nested-b/deeply-nested/foo.html", ""),
("nested-b/deeply-nested/bar.html", ""),
("nested-b/deeply-nested/baz.html", ""),
("nested-c/foo.html", ""),
("nested-c/bar.html", ""),
("nested-c/baz.html", ""),
("nested-c/.gitignore", "ignored-folder/"),
("nested-c/ignored-folder/foo.html", ""),
("nested-c/ignored-folder/bar.html", ""),
("nested-c/ignored-folder/baz.html", ""),
("nested-c/sibling-folder/foo.html", ""),
("nested-c/sibling-folder/bar.html", ""),
("nested-c/sibling-folder/baz.html", ""),
("nested-d/foo.html", ""),
("nested-d/bar.html", ""),
("nested-d/baz.html", ""),
("nested-d/.gitignore", "deep/"),
("nested-d/very/deeply/nested/deep/foo.html", ""),
("nested-d/very/deeply/nested/deep/bar.html", ""),
("nested-d/very/deeply/nested/deep/baz.html", ""),
("nested-d/very/deeply/nested/foo.html", ""),
("nested-d/very/deeply/nested/bar.html", ""),
("nested-d/very/deeply/nested/baz.html", ""),
("nested-d/very/deeply/nested/directory/foo.html", ""),
("nested-d/very/deeply/nested/directory/bar.html", ""),
("nested-d/very/deeply/nested/directory/baz.html", ""),
("nested-d/very/deeply/nested/directory/again/foo.html", ""),
]);
assert_eq!(
files,
vec![
"bar.html",
"baz.html",
"foo.html",
"nested-a/bar.html",
"nested-a/baz.html",
"nested-a/foo.html",
"nested-b/deeply-nested/bar.html",
"nested-b/deeply-nested/baz.html",
"nested-b/deeply-nested/foo.html",
"nested-c/bar.html",
"nested-c/baz.html",
"nested-c/foo.html",
"nested-c/sibling-folder/bar.html",
"nested-c/sibling-folder/baz.html",
"nested-c/sibling-folder/foo.html",
"nested-d/bar.html",
"nested-d/baz.html",
"nested-d/foo.html",
"nested-d/very/deeply/nested/bar.html",
"nested-d/very/deeply/nested/baz.html",
"nested-d/very/deeply/nested/directory/again/foo.html",
"nested-d/very/deeply/nested/directory/bar.html",
"nested-d/very/deeply/nested/directory/baz.html",
"nested-d/very/deeply/nested/directory/foo.html",
"nested-d/very/deeply/nested/foo.html",
]
);
assert_eq!(globs, vec![
"*",
"nested-a/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-b/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-c/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-c/sibling-folder/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-d/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-d/very/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-d/very/deeply/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-d/very/deeply/nested/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"nested-d/very/deeply/nested/directory/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_scan_for_utilities() {
let mut ignores = String::new();
ignores.push_str("# md:font-bold\n");
ignores.push_str("foo.html\n");
let ScanResult {
candidates,
normalized_sources,
..
} = scan(&[
(".gitignore", &ignores),
("index.html", "font-bold md:flex"),
("foo.jpg", "xl:font-bold"),
("foo.html", "lg:font-bold"),
(
"index.angular.html",
"<div [class.underline]=\"bool\"></div>",
),
("index.svelte", "<div class:px-4='condition'></div>"),
("index2.svelte", "<div\n\tclass:px-5='condition'></div>"),
("index3.svelte", "<div\n class:px-6='condition'></div>"),
("index4.svelte", "<div\nclass:px-7='condition'></div>"),
]);
assert_eq!(
candidates,
vec![
"bool",
"class",
"condition",
"font-bold",
"md:flex",
"px-4",
"px-5",
"px-6",
"px-7",
"underline"
]
);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn it_should_be_possible_to_scan_in_the_parent_directory() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[("foo/bar/baz/foo.html", "content-['foo.html']")],
vec!["@source '**/*'", "@source './foo/bar/baz/..'"],
);
assert_eq!(candidates, vec!["content-['foo.html']"]);
assert_eq!(normalized_sources, vec!["**/*", "foo/bar/**/*"]);
}
#[test]
fn it_should_scan_files_without_extensions() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[("my-file", "content-['my-file']")],
vec!["@source '**/*'", "@source './my-file'"],
);
assert_eq!(candidates, vec!["content-['my-file']"]);
assert_eq!(normalized_sources, vec!["**/*", "my-file"]);
}
#[test]
fn it_should_scan_folders_with_extensions() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[
(
"my-folder.templates/foo.html",
"content-['my-folder.templates/foo.html']",
),
(
"my-folder.bin/foo.html",
"content-['my-folder.bin/foo.html']",
),
],
vec![
"@source '**/*'",
"@source './my-folder.templates'",
"@source './my-folder.bin'",
],
);
assert_eq!(
candidates,
vec![
"content-['my-folder.bin/foo.html']",
"content-['my-folder.templates/foo.html']",
]
);
assert_eq!(
normalized_sources,
vec!["**/*", "my-folder.bin/**/*", "my-folder.templates/**/*"]
);
}
#[test]
fn it_should_scan_content_paths() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[
("foo.styl", "content-['foo.styl']"),
],
vec!["@source '**/*'", "@source '*.styl'"],
);
assert_eq!(candidates, vec!["content-['foo.styl']"]);
assert_eq!(normalized_sources, vec!["**/*", "*.styl"]);
}
#[test]
fn it_should_scan_next_dynamic_folders() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[
("app/[slug]/page.styl", "content-['[slug]']"),
("app/[...slug]/page.styl", "content-['[...slug]']"),
("app/[[...slug]]/page.styl", "content-['[[...slug]]']"),
("app/(theme)/page.styl", "content-['(theme)']"),
],
vec!["@source '**/*'", "@source './**/*.{styl}'"],
);
assert_eq!(
candidates,
vec![
"content-['(theme)']",
"content-['[...slug]']",
"content-['[[...slug]]']",
"content-['[slug]']",
],
);
assert_eq!(normalized_sources, vec!["**/*", "**/*.styl"]);
}
#[test]
fn it_should_scan_absolute_paths() {
let dir = tempdir().unwrap().into_path();
let _ = Command::new("git").arg("init").current_dir(&dir).output();
create_files_in(
&dir,
&[
("project-a/index.html", "content-['project-a/index.html']"),
("project-b/index.html", "content-['project-b/index.html']"),
],
);
let full_path = format!("{}", dir.display()).replace('\\', "/");
let sources = vec![PublicSourceEntry {
base: full_path.clone(),
pattern: full_path.clone(),
negated: false,
}];
let mut scanner = Scanner::new(sources);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['project-a/index.html']",
"content-['project-b/index.html']"
]
);
}
#[test]
fn it_should_scan_content_paths_even_when_they_are_git_ignored() {
let ScanResult {
candidates,
normalized_sources,
..
} = scan_with_globs(
&[
(".gitignore", "foo.styl"),
("foo.styl", "content-['foo.styl']"),
],
vec!["@source '**/*'", "@source 'foo.styl'"],
);
assert_eq!(candidates, vec!["content-['foo.styl']"]);
assert_eq!(normalized_sources, vec!["**/*", "foo.styl"]);
}
#[test]
fn it_should_pick_up_new_files() {
let dir = tempdir().unwrap().into_path();
let _ = Command::new("git").arg("init").current_dir(&dir).output();
create_files_in(
&dir,
&[
("project-a/index.html", "content-['project-a/index.html']"),
("project-b/index.html", "content-['project-b/index.html']"),
],
);
let sources = vec![
public_source_entry_from_pattern(dir.join("project-a"), "@source '**/*'"),
public_source_entry_from_pattern(dir.join("project-b"), "@source '**/*'"),
];
let mut scanner = Scanner::new(sources);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['project-a/index.html']",
"content-['project-b/index.html']"
]
);
sleep(Duration::from_millis(100));
create_files_in(
&dir,
&[
("project-a/new.html", "content-['project-a/new.html']"),
("project-b/new.html", "content-['project-b/new.html']"),
],
);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['project-a/index.html']",
"content-['project-a/new.html']",
"content-['project-b/index.html']",
"content-['project-b/new.html']"
]
);
sleep(Duration::from_millis(100));
create_files_in(
&dir,
&[
(
"project-a/sub1/sub2/index.html",
"content-['project-a/sub1/sub2/index.html']",
),
(
"project-b/sub1/sub2/index.html",
"content-['project-b/sub1/sub2/index.html']",
),
],
);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['project-a/index.html']",
"content-['project-a/new.html']",
"content-['project-a/sub1/sub2/index.html']",
"content-['project-b/index.html']",
"content-['project-b/new.html']",
"content-['project-b/sub1/sub2/index.html']"
]
);
sleep(Duration::from_millis(100));
create_files_in(
&dir,
&[
(
"project-a/sub1/sub2/new.html",
"content-['project-a/sub1/sub2/new.html']",
),
(
"project-b/sub1/sub2/new.html",
"content-['project-b/sub1/sub2/new.html']",
),
],
);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['project-a/index.html']",
"content-['project-a/new.html']",
"content-['project-a/sub1/sub2/index.html']",
"content-['project-a/sub1/sub2/new.html']",
"content-['project-b/index.html']",
"content-['project-b/new.html']",
"content-['project-b/sub1/sub2/index.html']",
"content-['project-b/sub1/sub2/new.html']"
]
);
}
#[test]
fn it_should_ignore_negated_custom_sources() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
("src/index.ts", "content-['src/index.ts']"),
("src/colors/red.jsx", "content-['src/colors/red.jsx']"),
("src/colors/blue.tsx", "content-['src/colors/blue.tsx']"),
("src/colors/green.tsx", "content-['src/colors/green.tsx']"),
("src/utils/string.ts", "content-['src/utils/string.ts']"),
("src/utils/date.ts", "content-['src/utils/date.ts']"),
("src/utils/file.ts", "content-['src/utils/file.ts']"),
(
"src/admin/foo/template.html",
"content-['src/admin/template.html']",
),
(
"src/templates/index.html",
"content-['src/templates/index.html']",
),
],
vec![
"@source '**/*'",
"@source not 'src/index.ts'",
"@source not '**/*.{jsx,tsx}'",
"@source not 'src/utils'",
"@source not 'dist'",
],
);
assert_eq!(
candidates,
vec![
"content-['src/admin/template.html']",
"content-['src/templates/index.html']",
]
);
assert_eq!(
files,
vec![
"src/admin/foo/template.html",
"src/templates/index.html",
]
);
assert_eq!(
globs,
vec![
"*",
"src/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"src/admin/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"src/colors/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"src/templates/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
]
);
assert_eq!(normalized_sources, vec!["**/*",]);
}
#[test]
fn it_should_include_defined_extensions_that_are_ignored_by_default() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
("src/index.exe", "content-['src/index.exe']"),
("src/index.bin", "content-['src/index.bin']"),
("out/out.exe", "content-['out/out.exe']"),
],
vec!["@source '**/*'", "@source 'src/**/*.{exe,bin}'"],
);
assert_eq!(
candidates,
vec!["content-['src/index.bin']", "content-['src/index.exe']",]
);
assert_eq!(files, vec!["src/index.bin", "src/index.exe",]);
assert_eq!(
globs,
vec![
"*",
"out/**/*.{aspx,astro,bin,cjs,cts,eex,erb,exe,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}",
"src/{**/*.bin,**/*.exe,**/*.{aspx,astro,bin,cjs,cts,eex,erb,exe,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}}",
]
);
assert_eq!(
normalized_sources,
vec!["**/*", "src/**/*.bin", "src/**/*.exe"]
);
}
#[test]
fn it_should_work_with_manual_glob_only() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
("index.html", "content-['index.html']"),
("src/index.html", "content-['src/index.html']"),
("src/ignore.html", "content-['src/ignore.html']"),
("src/admin/index.html", "content-['src/admin/index.html']"),
("src/admin/ignore.html", "content-['src/admin/ignore.html']"),
(
"src/dashboard/index.html",
"content-['src/dashboard/index.html']",
),
(
"src/dashboard/ignore.html",
"content-['src/dashboard/ignore.html']",
),
("src/lib.ts", "content-['src/lib.ts']"),
],
vec![
"@source './src/**/*.html'",
"@source not './src/index.html'",
"@source not './src/**/ignore.html'",
],
);
assert_eq!(
candidates,
vec![
"content-['src/admin/index.html']",
"content-['src/dashboard/index.html']",
]
);
assert_eq!(
files,
vec!["src/admin/index.html", "src/dashboard/index.html",]
);
assert_eq!(globs, vec!["src/**/*.html"]);
assert_eq!(normalized_sources, vec!["src/**/*.html"]);
}
#[test]
fn it_respects_gitignore_in_workspace_root() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
(".gitignore", "ignore-1.html\nweb/ignore-2.html"),
("src/index.html", "content-['src/index.html']"),
("web/index.html", "content-['web/index.html']"),
("web/ignore-1.html", "content-['web/ignore-1.html']"),
("web/ignore-2.html", "content-['web/ignore-2.html']"),
],
vec!["@source './src'", "@source './web'"],
);
assert_eq!(
candidates,
vec!["content-['src/index.html']", "content-['web/index.html']",]
);
assert_eq!(files, vec!["src/index.html", "web/index.html",]);
assert_eq!(globs, vec!["src/*", "web/*",]);
assert_eq!(normalized_sources, vec!["src/**/*", "web/**/*",]);
}
#[test]
fn it_includes_skipped_by_default_extensions_with_a_specific_source() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
("src/logo.jpg", "content-['/src/logo.jpg']"),
("src/logo.png", "content-['/src/logo.png']"),
],
vec!["@source './src/logo.{jpg,png}'"],
);
assert_eq!(
candidates,
vec!["content-['/src/logo.jpg']", "content-['/src/logo.png']"]
);
assert_eq!(files, vec!["src/logo.jpg", "src/logo.png"]);
assert!(globs.is_empty());
assert_eq!(normalized_sources, vec!["src/logo.jpg", "src/logo.png"]);
}
#[test]
fn it_respects_gitignore_in_workspace_root_for_manual_globs() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
(".gitignore", "ignore-1.html\n/web/ignore-2.html"),
("web/index.html", "content-['web/index.html']"),
("web/ignore-1.html", "content-['web/ignore-1.html']"),
("web/ignore-2.html", "content-['web/ignore-2.html']"),
],
vec!["@source './web'", "@source './web/ignore-1.html'"],
);
assert_eq!(
candidates,
vec![
"content-['web/ignore-1.html']",
"content-['web/index.html']",
]
);
assert_eq!(files, vec!["web/ignore-1.html", "web/index.html",]);
assert_eq!(globs, vec!["web/*"]);
assert_eq!(normalized_sources, vec!["web/**/*", "web/ignore-1.html"]);
}
#[test]
fn skips_ignore_files_outside_of_a_repo() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("home/project/apps/web/index.html", "content-['index.html']"),
("home/.gitignore", "ignore-home.html"),
("home/project/.gitignore", "ignore-project.html"),
("home/project/apps/.gitignore", "ignore-apps.html"),
("home/project/apps/web/.gitignore", "ignore-web.html"),
(
"home/project/apps/web/ignore-home.html",
"content-['ignore-home.html']",
),
(
"home/project/apps/web/ignore-project.html",
"content-['ignore-project.html']",
),
(
"home/project/apps/web/ignore-apps.html",
"content-['ignore-apps.html']",
),
(
"home/project/apps/web/ignore-web.html",
"content-['ignore-web.html']",
),
(
"home/project/apps/admin/index.html",
"content-['home/project/apps/admin/index.html']",
),
(
"home/project/apps/dashboard/index.html",
"content-['home/project/apps/dashboard/index.html']",
),
],
);
let sources = vec![
public_source_entry_from_pattern(
dir.join("home/project/apps/web")
.to_string_lossy()
.to_string()
.into(),
"@source '**/*'",
),
public_source_entry_from_pattern(
dir.join("home/project/apps/web")
.to_string_lossy()
.to_string()
.into(),
"@source '../admin'",
),
public_source_entry_from_pattern(
dir.join("home/project/apps/web")
.to_string_lossy()
.to_string()
.into(),
"@source '../dashboard/*.html'",
),
];
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(
candidates,
vec![
"content-['home/project/apps/admin/index.html']",
"content-['home/project/apps/dashboard/index.html']",
"content-['index.html']"
]
);
_ = Command::new("git")
.arg("init")
.current_dir(dir.join("home"))
.output();
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(
candidates,
vec![
"content-['home/project/apps/admin/index.html']",
"content-['home/project/apps/dashboard/index.html']",
"content-['index.html']"
]
);
fs::remove_dir_all(dir.join("home/.git")).unwrap();
_ = Command::new("git")
.arg("init")
.current_dir(dir.join("home/project"))
.output();
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(
candidates,
vec![
"content-['home/project/apps/admin/index.html']",
"content-['home/project/apps/dashboard/index.html']",
"content-['ignore-home.html']",
"content-['index.html']"
]
);
fs::remove_dir_all(dir.join("home/project/.git")).unwrap();
_ = Command::new("git")
.arg("init")
.current_dir(dir.join("home/project/apps"))
.output();
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(
candidates,
vec![
"content-['home/project/apps/admin/index.html']",
"content-['home/project/apps/dashboard/index.html']",
"content-['ignore-home.html']",
"content-['ignore-project.html']",
"content-['index.html']"
]
);
fs::remove_dir_all(dir.join("home/project/apps/.git")).unwrap();
_ = Command::new("git")
.arg("init")
.current_dir(dir.join("home/project/apps/web"))
.output();
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(
candidates,
vec![
"content-['home/project/apps/admin/index.html']",
"content-['home/project/apps/dashboard/index.html']",
"content-['ignore-apps.html']",
"content-['ignore-home.html']",
"content-['ignore-project.html']",
"content-['index.html']",
]
);
}
#[test]
fn test_explicitly_ignore_explicitly_allowed_files() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("src/keep-me.html", "content-['keep-me.html']"),
("src/ignore-me.html", "content-['ignore-me.html']"),
],
);
let sources = vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*.html'"),
public_source_entry_from_pattern(dir.clone(), "@source not 'src/ignore-me.html'"),
];
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(candidates, vec!["content-['keep-me.html']"]);
}
#[test]
fn test_works_with_filenames_containing_glob_characters() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("src/app/[foo]/ignore-me.html", "content-['ignore-me.html']"),
("src/app/[foo]/keep-me.html", "content-['keep-me.html']"),
],
);
let sources = vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*'"),
public_source_entry_from_pattern(
dir.clone(),
"@source not 'src/app/[foo]/ignore*.html'",
),
];
let candidates = Scanner::new(sources.clone()).scan();
assert_eq!(candidates, vec!["content-['keep-me.html']"]);
}
#[test]
fn test_ignore_files_can_be_included_with_custom_source_rule() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[("src/keep-me.html", "content-['src/keep-me.html']")],
);
let mut scanner = Scanner::new(vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*.html'"),
public_source_entry_from_pattern(
dir.clone(),
"@source not 'src/ignored-by-source-not.html'",
),
]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['src/keep-me.html']"]);
create_files_in(
&dir,
&[
("src/new-file.html", "content-['src/new-file.html']"),
(
"src/ignore-by-extension.bin",
"content-['src/ignore-by-extension.bin']",
),
(".gitignore", "src/ignored-by-gitignore.html"),
(
"src/ignored-by-gitignore.html",
"content-['src/ignored-by-gitignore.html']",
),
(
"src/ignored-by-source-not.html",
"content-['src/ignored-by-source-not.html']",
),
],
);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['src/ignored-by-gitignore.html']",
"content-['src/keep-me.html']",
"content-['src/new-file.html']"
]
);
}
#[test]
fn test_allow_default_ignored_files() {
let dir = tempdir().unwrap().into_path();
create_files_in(&dir, &[("foo.styl", "content-['foo.styl']")]);
let sources = vec![public_source_entry_from_pattern(
dir.clone(),
"@source '**/*'",
)];
let mut scanner = Scanner::new(sources.clone());
let candidates = scanner.scan();
assert!(candidates.is_empty());
let mut scanner = Scanner::new(vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*'"),
public_source_entry_from_pattern(dir.clone(), "@source '*.styl'"),
]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['foo.styl']"]);
}
#[test]
fn test_allow_default_ignored_files_via_gitignore() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("index.html", "content-['index.html']"),
(".gitignore", "index.html"),
],
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source '**/*'",
)]);
let candidates = scanner.scan();
assert!(candidates.is_empty());
let mut scanner = Scanner::new(vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*'"),
public_source_entry_from_pattern(dir.clone(), "@source './*.html'"),
]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['index.html']"]);
}
#[test]
fn test_allow_explicit_node_modules_paths() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("src/index.html", "content-['src/index.html']"),
(".gitignore", "node_modules"),
(
"node_modules/my-ui-lib/index.html",
"content-['node_modules/my-ui-lib/index.html']",
),
],
);
let sources = vec![public_source_entry_from_pattern(
dir.clone(),
"@source './'",
)];
let mut scanner = Scanner::new(sources.clone());
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['src/index.html']"]);
let sources = vec![public_source_entry_from_pattern(
dir.clone(),
"@source '**/*.html'",
)];
let mut scanner = Scanner::new(sources.clone());
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['src/index.html']"]);
let sources = vec![
public_source_entry_from_pattern(dir.clone(), "@source '**/*.html'"),
public_source_entry_from_pattern(dir.clone(), "@source 'node_modules/my-ui-lib'"),
];
let mut scanner = Scanner::new(sources.clone());
let candidates = scanner.scan();
assert_eq!(
candidates,
vec![
"content-['node_modules/my-ui-lib/index.html']",
"content-['src/index.html']"
]
);
}
#[test]
fn test_ignore_node_modules_without_gitignore() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
(
"packages/web/index.html",
"content-['packages/web/index.html']",
),
(
"node_modules/index.html",
"content-['node_modules/index.html']",
),
(
"packages/web/node_modules/index.html",
"content-['packages/web/node_modules/index.html']",
),
],
vec!["@source '**/*'"],
);
assert_eq!(candidates, vec!["content-['packages/web/index.html']"]);
assert_eq!(files, vec!["packages/web/index.html",]);
assert_eq!(globs, vec!["*", "packages/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}", "packages/web/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn test_ignore_gitignore_in_node_modules_source() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
(".gitignore", "node_modules\ndist"),
(
"node_modules/my-ui-lib/dist/index.html",
"content-['node_modules/my-ui-lib/dist/index.html']",
),
(
"node_modules/my-ui-lib/node.exe",
"content-['node_modules/my-ui-lib/node.exe']",
),
],
vec!["@source 'node_modules/my-ui-lib'"],
);
assert_eq!(
candidates,
vec!["content-['node_modules/my-ui-lib/dist/index.html']"]
);
assert_eq!(files, vec!["node_modules/my-ui-lib/dist/index.html"]);
assert_eq!(globs, vec!["node_modules/my-ui-lib/*", "node_modules/my-ui-lib/dist/**/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}"]);
assert_eq!(normalized_sources, vec!["node_modules/my-ui-lib/**/*"]);
}
#[test]
fn test_manually_scanning_files_should_follow_all_rules() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
(".gitignore", "*.jsx\ngenerated/"),
(
"src/components/button.tsx",
"content-['src/components/button.tsx']",
),
(
"src/components/button.jsx",
"content-['src/components/button.jsx']",
),
],
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source '**/*'",
)]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['src/components/button.tsx']"]);
create_files_in(
&dir,
&[
(
"src/components/good.tsx",
"content-['src/components/good.tsx']",
),
(
"src/components/bad.jsx",
"content-['src/components/bad.jsx']",
),
],
);
let candidates = scanner.scan_content(vec![
ChangedContent::File(dir.join("src/components/good.tsx"), "tsx".to_owned()),
ChangedContent::File(dir.join("src/components/bad.jsx"), "jsx".to_owned()),
]);
assert_eq!(candidates, vec!["content-['src/components/good.tsx']"]);
create_files_in(
&dir,
&[
(
"src/components/generated/bad.tsx",
"content-['src/components/generated/bad.tsx']",
),
(
"src/components/generated/bad.jsx",
"content-['src/components/generated/bad.jsx']",
),
],
);
let candidates = scanner.scan_content(vec![
ChangedContent::File(
dir.join("src/components/generated/bad.tsx"),
"tsx".to_owned(),
),
ChangedContent::File(
dir.join("src/components/generated/bad.jsx"),
"jsx".to_owned(),
),
]);
assert!(candidates.is_empty());
}
#[test]
fn test_works_with_utf8_special_character_paths() {
let ScanResult {
candidates,
files,
globs,
normalized_sources,
} = scan_with_globs(
&[
("src/💩.js", "content-['src/💩.js']"),
("src/🤦♂️.tsx", "content-['src/🤦♂️.tsx']"),
("src/🤦♂️/foo.tsx", "content-['src/🤦♂️/foo.tsx']"),
],
vec!["@source '**/*'", "@source not 'src/🤦♂️'"],
);
assert_eq!(
candidates,
vec!["content-['src/💩.js']", "content-['src/🤦♂️.tsx']"]
);
assert_eq!(files, vec!["src/💩.js", "src/🤦♂️.tsx"]);
assert_eq!(globs, vec!["*", "src/*/*.{aspx,astro,cjs,cts,eex,erb,gjs,gts,haml,handlebars,hbs,heex,html,jade,js,jsx,liquid,md,mdx,mjs,mts,mustache,njk,nunjucks,php,pug,py,razor,rb,rhtml,rs,slim,svelte,tpl,ts,tsx,twig,vue}"]);
assert_eq!(normalized_sources, vec!["**/*"]);
}
#[test]
fn test_glob_with_symlinks() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
(".gitignore", "node_modules\ndist"),
(
"node_modules/.pnpm/@org+my-ui-library/dist/index.ts",
"content-['node_modules/.pnpm/@org+my-ui-library/dist/index.ts']",
),
("node_modules/@org/.gitkeep", ""),
],
);
let _ = symlink(
dir.join("node_modules/.pnpm/@org+my-ui-library"),
dir.join("node_modules/@org/my-ui-library"),
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source 'node_modules'",
)]);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec!["content-['node_modules/.pnpm/@org+my-ui-library/dist/index.ts']"]
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source 'node_modules/@org/my-ui-library'",
)]);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec!["content-['node_modules/.pnpm/@org+my-ui-library/dist/index.ts']"]
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source 'node_modules/@org'",
)]);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec!["content-['node_modules/.pnpm/@org+my-ui-library/dist/index.ts']"]
);
}
#[test]
fn test_globs_with_recursive_symlinks() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
("b/index.html", "content-['b/index.html']"),
("z/index.html", "content-['z/index.html']"),
],
);
let _ = symlink(dir.join("a"), dir.join("b"));
let _ = symlink(dir.join("b/c"), dir.join("c"));
let _ = symlink(dir.join("b/root"), &dir);
let _ = symlink(dir.join("c"), dir.join("a"));
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source '.'",
)]);
let candidates = scanner.scan();
assert_eq!(
candidates,
vec!["content-['b/index.html']", "content-['z/index.html']"]
);
}
#[test]
fn test_partial_globs_with_symlinks() {
let dir = tempdir().unwrap().into_path();
create_files_in(&dir, &[("abcd/xyz.html", "content-['abcd/xyz.html']")]);
let _ = symlink(dir.join("abcd"), dir.join("efgh"));
let mut scanner = Scanner::new(vec![]);
let candidates = scanner.scan();
assert!(candidates.is_empty());
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source 'efgh/*.html'",
)]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['abcd/xyz.html']"]);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source 'ef*/*.html'",
)]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["content-['abcd/xyz.html']"]);
}
#[test]
fn test_extract_used_css_variables_from_css() {
let dir = tempdir().unwrap().into_path();
create_files_in(
&dir,
&[
(
"src/index.css",
r#"
@theme {
--color-red: #ff0000; /* Not used, so don't extract */
--color-green: #00ff00; /* Not used, so don't extract */
}
.button {
color: var(--color-red); /* Used, so extract */
}
"#,
),
("src/used-at-start.css", "var(--color-used-at-start)"),
("src/defined-at-start.css", "--color-defined-at-start: red;"),
],
);
let mut scanner = Scanner::new(vec![public_source_entry_from_pattern(
dir.clone(),
"@source './'",
)]);
let candidates = scanner.scan();
assert_eq!(candidates, vec!["--color-red", "--color-used-at-start"]);
}
}