Skip to content

Commit 374c901

Browse files
authored
test: add more spec tests (#9)
* test: add logger tests * test: add legacy render tests
1 parent 4272e9a commit 374c901

File tree

14 files changed

+1455
-27
lines changed

14 files changed

+1455
-27
lines changed

.github/workflows/ci.yml

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,10 @@ jobs:
4646
test:
4747
name: 'Tests | ${{ matrix.os }}'
4848
runs-on: ${{ matrix.os }}-latest
49-
5049
strategy:
5150
matrix:
5251
os: [ubuntu, macos, windows]
5352
fail-fast: false
54-
5553
steps:
5654
- uses: actions/checkout@v3
5755
- uses: arduino/setup-protoc@v1
@@ -70,7 +68,26 @@ jobs:
7068
toolchain: 'stable'
7169
profile: minimal
7270
override: true
73-
- name: test default features
74-
run: cargo test -- --nocapture
75-
- name: test legacy features
76-
run: cargo test --features legacy -- --test-threads=1 --nocapture
71+
# Since cargo test will run tests in parallel, and the crate uses cwd in
72+
# legacy importer and captured stdio for testing logger, so we need to
73+
# run tests in sequentially by adding `--test-threads=1`
74+
- run: cargo test --all-features -- --test-threads=1 --nocapture
75+
76+
publish:
77+
name: Publish
78+
runs-on: ubuntu-latest
79+
steps:
80+
- uses: actions/checkout@v3
81+
- uses: actions-rs/toolchain@v1
82+
with:
83+
toolchain: 'stable'
84+
profile: minimal
85+
override: true
86+
- uses: obi1kenobi/cargo-semver-checks-action@v1
87+
- name: publish
88+
if: |
89+
github.repository == 'ahabhgk/sass-embedded-host-rust' &&
90+
startsWith(github.ref, 'refs/tags/')
91+
env:
92+
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
93+
run: cargo publish -vv

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ urlencoding = { version = "2", optional = true }
2323
[dev-dependencies]
2424
serde_json = "1"
2525
tempfile = "3"
26+
gag = "1"
27+
pathdiff = "0.2"
2628

2729
[build-dependencies]
2830
prost-build = "0.11"

src/api.rs

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ impl OptionsBuilder {
126126
self
127127
}
128128

129-
pub fn logger(mut self, arg: impl Into<SassLogger>) -> Self {
130-
self.options.logger = Some(arg.into());
129+
pub fn logger<L: 'static + Logger>(mut self, arg: L) -> Self {
130+
self.options.logger = Some(Box::new(arg));
131131
self
132132
}
133133

@@ -275,8 +275,8 @@ impl StringOptionsBuilder {
275275
self
276276
}
277277

278-
pub fn logger(mut self, arg: impl Into<SassLogger>) -> Self {
279-
self.options.logger = Some(arg.into());
278+
pub fn logger<L: 'static + Logger>(mut self, arg: L) -> Self {
279+
self.options.logger = Some(Box::new(arg));
280280
self
281281
}
282282

@@ -313,19 +313,25 @@ impl StringOptionsBuilder {
313313
pub type SassLogger = Box<dyn Logger>;
314314

315315
pub trait Logger: Debug + Send + Sync {
316-
fn warn(&self, message: &str, options: &LoggerWarnOptions);
316+
fn warn(&self, _message: &str, options: &LoggerWarnOptions) {
317+
eprintln!("{}", options.formatted);
318+
}
317319

318-
fn debug(&self, message: &str, options: &LoggerDebugOptions);
320+
fn debug(&self, _message: &str, options: &LoggerDebugOptions) {
321+
eprintln!("{}", options.formatted);
322+
}
319323
}
320324

321325
pub struct LoggerWarnOptions {
322326
pub deprecation: bool,
323327
pub span: Option<SourceSpan>,
324328
pub stack: Option<String>,
329+
pub(crate) formatted: String,
325330
}
326331

327332
pub struct LoggerDebugOptions {
328333
pub span: Option<SourceSpan>,
334+
pub(crate) formatted: String,
329335
}
330336

331337
#[derive(Debug)]

src/error.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::protocol::{
66

77
pub type Result<T> = std::result::Result<T, Exception>;
88

9-
#[derive(Debug)]
9+
#[derive(Debug, Clone)]
1010
pub struct Exception {
1111
message: String,
1212
sass_message: Option<String>,

src/host/logger_registry.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ impl LoggerRegistry {
1616
pub fn log(&self, event: LogEvent) {
1717
if let Some(logger) = &self.logger {
1818
if event.r#type() == LogEventType::Debug {
19-
logger.debug(&event.message, &LoggerDebugOptions { span: event.span });
19+
logger.debug(
20+
&event.message,
21+
&LoggerDebugOptions {
22+
span: event.span,
23+
formatted: event.formatted,
24+
},
25+
);
2026
} else {
2127
let deprecation = event.r#type() == LogEventType::DeprecationWarning;
2228
logger.warn(
@@ -29,6 +35,7 @@ impl LoggerRegistry {
2935
} else {
3036
Some(event.stack_trace)
3137
},
38+
formatted: event.formatted,
3239
},
3340
);
3441
}

src/legacy.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ use std::{sync::Arc, time::SystemTime};
66
pub use api::*;
77
pub use importer::*;
88

9-
use crate::{Embedded, Importer, Options, Result, SassImporter, StringOptions};
9+
use crate::{
10+
Embedded, Exception, Importer, Options, Result, SassImporter, StringOptions,
11+
};
1012

1113
impl Embedded {
1214
pub fn render(&mut self, options: LegacyOptions) -> Result<LegacyResult> {
@@ -65,8 +67,18 @@ impl Embedded {
6567
options.importers = importers;
6668
self.compile(file, options)
6769
} else {
68-
panic!("Either options.data or options.file must be set.");
70+
Err(Exception::new(
71+
"Either options.data or options.file must be set.",
72+
))
6973
}?;
70-
Ok(LegacyResult::new(entry, start, result))
74+
Ok(LegacyResult::new(
75+
if entry == "stdin" {
76+
"data".to_string()
77+
} else {
78+
entry
79+
},
80+
start,
81+
result,
82+
))
7183
}
7284
}

src/legacy/api.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::{
1010
legacy::url_to_file_path_cross_platform, CompileResult, Options,
1111
StringOptions, Syntax, Url,
1212
};
13-
pub use crate::{OutputStyle, SassLogger};
13+
pub use crate::{Logger, OutputStyle, SassLogger};
1414

1515
use super::{
1616
LegacyImporter, SassLegacyImporter, END_OF_LOAD_PROTOCOL,
@@ -244,8 +244,8 @@ impl LegacyOptionsBuilder {
244244
self
245245
}
246246

247-
pub fn logger(mut self, arg: impl Into<SassLogger>) -> Self {
248-
self.options.logger = Some(arg.into());
247+
pub fn logger<L: 'static + Logger>(mut self, arg: L) -> Self {
248+
self.options.logger = Some(Box::new(arg));
249249
self
250250
}
251251

src/lib.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,3 +21,12 @@ pub use embedded::{Embedded, Embedded as Sass};
2121
pub use error::{Exception, Result};
2222
pub use protocol::{OutputStyle, SourceSpan, Syntax};
2323
pub use url::{self, Url};
24+
25+
#[derive(Debug, Default, Clone)]
26+
pub struct Silent;
27+
28+
impl Logger for Silent {
29+
fn debug(&self, _: &str, _: &LoggerDebugOptions) {}
30+
31+
fn warn(&self, _: &str, _: &LoggerWarnOptions) {}
32+
}

tests/compile_spec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#[path = "./helpers.rs"]
1+
#[path = "helpers.rs"]
22
mod helpers;
33

44
use helpers::{exe_path, Sandbox, ToUrl};

tests/helpers.rs

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
use std::{
22
env, fs,
3-
io::Write,
3+
io::{Read, Write},
44
path::{Path, PathBuf},
55
};
66

7+
use gag::BufferRedirect;
78
use sass_embedded_host_rust::{Sass, Url};
89
use tempfile::TempDir;
910

@@ -55,9 +56,6 @@ impl Sandbox {
5556
self
5657
}
5758

58-
// Since cargo test will run tests in parallel, and the host uses the
59-
// cwd when legacy is on, so we need to run legacy tests in sequentially
60-
// by adding `--test-threads=1`
6159
#[cfg(feature = "legacy")]
6260
#[allow(dead_code)]
6361
pub fn chdir(&self) -> ChdirGuard {
@@ -75,6 +73,24 @@ impl Drop for ChdirGuard {
7573
}
7674
}
7775

76+
#[derive(Debug, Clone)]
77+
pub struct Captured {
78+
pub out: String,
79+
pub err: String,
80+
}
81+
82+
#[allow(dead_code)]
83+
pub fn capture_stdio(f: impl Fn()) -> Captured {
84+
let mut stdout = BufferRedirect::stdout().unwrap();
85+
let mut stderr = BufferRedirect::stderr().unwrap();
86+
f();
87+
let mut out = String::new();
88+
let mut err = String::new();
89+
stdout.read_to_string(&mut out).unwrap();
90+
stderr.read_to_string(&mut err).unwrap();
91+
Captured { out, err }
92+
}
93+
7894
pub trait ToUrl {
7995
fn to_url(&self) -> Url;
8096
}

0 commit comments

Comments
 (0)