2022-02-27 18:31:19 +03:00
|
|
|
#![allow(clippy::needless_update)]
|
|
|
|
|
|
|
|
use std::{fs, path::PathBuf};
|
|
|
|
|
|
|
|
use swc_common::{errors::HANDLER, input::SourceFileInput};
|
|
|
|
use swc_css_lints::{get_rules, LintConfig, LintParams, LintRule};
|
2023-03-29 11:48:45 +03:00
|
|
|
use swc_css_parser::{
|
|
|
|
lexer::Lexer,
|
|
|
|
parser::{Parser, ParserConfig},
|
|
|
|
};
|
2022-02-27 18:31:19 +03:00
|
|
|
|
|
|
|
#[testing::fixture("tests/rules/pass/**/input.css")]
|
|
|
|
fn pass(input: PathBuf) {
|
|
|
|
let config_path = input.parent().unwrap().join("config.json");
|
|
|
|
let lint_config =
|
|
|
|
serde_json::from_str::<LintConfig>(&fs::read_to_string(config_path).unwrap()).unwrap();
|
|
|
|
|
|
|
|
testing::run_test2(false, |cm, handler| -> Result<(), _> {
|
2023-03-29 11:48:45 +03:00
|
|
|
let config = ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-02-27 18:31:19 +03:00
|
|
|
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
|
|
|
|
let mut parser = Parser::new(lexer, config);
|
|
|
|
|
|
|
|
let stylesheet = match parser.parse_all() {
|
|
|
|
Ok(stylesheet) => stylesheet,
|
|
|
|
Err(err) => {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rules = get_rules(&LintParams {
|
|
|
|
lint_config: &lint_config,
|
2022-03-05 21:31:52 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
2022-02-27 18:31:19 +03:00
|
|
|
|
|
|
|
HANDLER.set(&handler, || {
|
|
|
|
rules.lint_stylesheet(&stylesheet);
|
|
|
|
});
|
|
|
|
|
|
|
|
if handler.has_errors() {
|
|
|
|
return Err(());
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
})
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
#[testing::fixture("tests/rules/fail/**/input.css")]
|
|
|
|
fn fail(input: PathBuf) {
|
2022-11-29 12:47:56 +03:00
|
|
|
let stderr_path = input.parent().unwrap().join("output.swc-stderr");
|
2022-02-27 18:31:19 +03:00
|
|
|
let config_path = input.parent().unwrap().join("config.json");
|
|
|
|
let lint_config =
|
|
|
|
serde_json::from_str::<LintConfig>(&fs::read_to_string(config_path).unwrap()).unwrap();
|
|
|
|
|
|
|
|
let stderr = testing::run_test2(false, |cm, handler| -> Result<(), _> {
|
2023-03-29 11:48:45 +03:00
|
|
|
let config = ParserConfig {
|
|
|
|
..Default::default()
|
|
|
|
};
|
2022-02-27 18:31:19 +03:00
|
|
|
|
|
|
|
let fm = cm.load_file(&input).unwrap();
|
|
|
|
let lexer = Lexer::new(SourceFileInput::from(&*fm), config);
|
|
|
|
let mut parser = Parser::new(lexer, config);
|
|
|
|
|
|
|
|
let stylesheet = match parser.parse_all() {
|
|
|
|
Ok(stylesheet) => stylesheet,
|
|
|
|
Err(err) => {
|
|
|
|
err.to_diagnostics(&handler).emit();
|
|
|
|
panic!();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
let mut rules = get_rules(&LintParams {
|
|
|
|
lint_config: &lint_config,
|
2022-03-05 21:31:52 +03:00
|
|
|
})
|
|
|
|
.unwrap();
|
2022-02-27 18:31:19 +03:00
|
|
|
|
|
|
|
HANDLER.set(&handler, || {
|
|
|
|
rules.lint_stylesheet(&stylesheet);
|
|
|
|
});
|
|
|
|
|
|
|
|
if !handler.has_errors() {
|
|
|
|
panic!("should error");
|
|
|
|
}
|
|
|
|
|
|
|
|
Err(())
|
|
|
|
})
|
|
|
|
.unwrap_err();
|
|
|
|
|
2023-03-14 07:56:21 +03:00
|
|
|
stderr.compare_to_file(stderr_path).unwrap();
|
2022-02-27 18:31:19 +03:00
|
|
|
}
|