Skip to content

Commit d93be02

Browse files
committed
test: add for TomlLockfileSourceId
1 parent ee515e6 commit d93be02

File tree

1 file changed

+80
-0
lines changed

1 file changed

+80
-0
lines changed

crates/cargo-util-schemas/src/lockfile.rs

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,3 +317,83 @@ fn dump_lockfile_schema() {
317317
let dump = serde_json::to_string_pretty(&schema).unwrap();
318318
snapbox::assert_data_eq!(dump, snapbox::file!("../lockfile.schema.json").raw());
319319
}
320+
321+
#[cfg(test)]
322+
mod tests {
323+
use crate::core::{GitReference, SourceKind};
324+
use crate::lockfile::{EncodableSourceIdErrorKind, TomlLockfileSourceId};
325+
326+
#[track_caller]
327+
fn ok(source_str: &str, source_kind: SourceKind, url: &str) {
328+
let source_str = source_str.to_owned();
329+
let source_id = TomlLockfileSourceId::new(source_str).unwrap();
330+
assert_eq!(source_id.kind, source_kind);
331+
assert_eq!(source_id.url().to_string(), url);
332+
}
333+
334+
macro_rules! err {
335+
($src:expr, $expected:pat) => {
336+
let kind = TomlLockfileSourceId::new($src.to_owned()).unwrap_err().0;
337+
assert!(
338+
matches!(kind, $expected),
339+
"`{}` parse error mismatch, got {kind:?}",
340+
$src,
341+
);
342+
};
343+
}
344+
345+
#[test]
346+
fn good_sources() {
347+
ok(
348+
"sparse+https://my-crates.io",
349+
SourceKind::SparseRegistry,
350+
"https://my-crates.io/",
351+
);
352+
ok(
353+
"registry+https://github.com/rust-lang/crates.io-index",
354+
SourceKind::Registry,
355+
"https://github.com/rust-lang/crates.io-index",
356+
);
357+
ok(
358+
"git+https://github.com/rust-lang/cargo",
359+
SourceKind::Git(GitReference::DefaultBranch),
360+
"https://github.com/rust-lang/cargo",
361+
);
362+
ok(
363+
"git+https://github.com/rust-lang/cargo?branch=dev",
364+
SourceKind::Git(GitReference::Branch("dev".to_owned())),
365+
"https://github.com/rust-lang/cargo?branch=dev",
366+
);
367+
ok(
368+
"git+https://github.com/rust-lang/cargo?tag=v1.0",
369+
SourceKind::Git(GitReference::Tag("v1.0".to_owned())),
370+
"https://github.com/rust-lang/cargo?tag=v1.0",
371+
);
372+
ok(
373+
"git+https://github.com/rust-lang/cargo?rev=refs/pull/493/head",
374+
SourceKind::Git(GitReference::Rev("refs/pull/493/head".to_owned())),
375+
"https://github.com/rust-lang/cargo?rev=refs/pull/493/head",
376+
);
377+
ok(
378+
"path+file:///path/to/root",
379+
SourceKind::Path,
380+
"file:///path/to/root",
381+
);
382+
}
383+
384+
#[test]
385+
fn bad_sources() {
386+
err!(
387+
"unknown+https://my-crates.io",
388+
EncodableSourceIdErrorKind::UnsupportedSource(..)
389+
);
390+
err!(
391+
"registry+https//github.com/rust-lang/crates.io-index",
392+
EncodableSourceIdErrorKind::InvalidUrl { .. }
393+
);
394+
err!(
395+
"https//github.com/rust-lang/crates.io-index",
396+
EncodableSourceIdErrorKind::InvalidSource(..)
397+
);
398+
}
399+
}

0 commit comments

Comments
 (0)