Skip to content

Commit

Permalink
[WIP] exclude test code from coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMason committed Sep 30, 2024
1 parent 369d9d7 commit 0da933c
Show file tree
Hide file tree
Showing 20 changed files with 1,426 additions and 1,432 deletions.
4 changes: 2 additions & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ run-ci-action:
# Run all tests and generate a coverage report
coverage:
rm -rf target/llvm-cov/html
cargo llvm-cov test --html
cargo llvm-cov test --html --ignore-filename-regex '(_test.rs|\/test\/)'

# Open coverage report (on http server to allow Dark Reader Browser Extension)
serve-coverage:
Expand All @@ -94,7 +94,7 @@ watch pattern=no_pattern:

# Run test in watch mode with coverage
watch-coverage:
tput rmam && cargo watch --clear --exec 'llvm-cov test --html -- --nocapture --color=always'
tput rmam && cargo watch --clear --exec 'llvm-cov test --html --ignore-filename-regex '(_test.rs|\/test\/)' -- --nocapture --color=always'

# Run the rust binary against an unformatted test fixture
run-misc:
Expand Down
18 changes: 0 additions & 18 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,6 @@ pub struct Cli {
}

impl Cli {
#[cfg(test)]
pub fn new() -> Self {
Cli {
command_name: Subcommand::Lint,
options: CliOptions::new(),
}
}

pub fn parse() -> Cli {
match create().get_matches().subcommand() {
Some(("lint", matches)) => Cli {
Expand Down Expand Up @@ -138,16 +130,6 @@ pub struct CliOptions {
}

impl CliOptions {
#[cfg(test)]
pub fn new() -> Self {
Self {
filter: None,
format: false,
versions: true,
source: vec![],
}
}

/// Create a new `CliOptions` from CLI arguments provided by the user
pub fn from_arg_matches(matches: &ArgMatches) -> CliOptions {
// 1. if no command is true, then all of them are true
Expand Down
25 changes: 0 additions & 25 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,6 @@ impl Rcfile {
serde_json::from_str::<Self>(&empty_json).unwrap()
}

#[cfg(test)]
pub fn from_mock(value: serde_json::Value) -> Self {
serde_json::from_value::<Self>(value).unwrap()
}

/// Read a rcfile from the given location
pub fn from_file(file_path: &PathBuf) -> Option<Self> {
fs::read_to_string(file_path)
Expand Down Expand Up @@ -277,26 +272,6 @@ pub struct Config {
}

impl Config {
/// Create an empty struct
#[cfg(test)]
pub fn new() -> Self {
Self {
cli: Cli::new(),
cwd: std::env::current_dir().unwrap(),
rcfile: Rcfile::new(),
}
}

/// Create a struct from a mocked .syncpackrc
#[cfg(test)]
pub fn from_mock(value: serde_json::Value) -> Self {
Self {
cli: Cli::new(),
cwd: std::env::current_dir().unwrap(),
rcfile: Rcfile::from_mock(value),
}
}

/// Try to read the rcfile from the current working directory and fall back to
/// defaults if one is not found
pub fn from_cli(cwd: PathBuf, cli: Cli) -> Config {
Expand Down
1 change: 0 additions & 1 deletion src/effects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{

pub mod fix;
pub mod lint;
pub mod mock;

/// Side effects in Syncpack commands are handled by structs which implement
/// this trait. Multiple commands such as `lint`, `fix`, and `json` all depend
Expand Down
272 changes: 4 additions & 268 deletions src/format.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#[cfg(test)]
#[path = "format_test.rs"]
mod format_test;

use icu::{
collator::{Collator, CollatorOptions},
locid::{locale, Locale},
Expand Down Expand Up @@ -159,271 +163,3 @@ fn get_locale_collator() -> Collator {
let collator: Collator = Collator::try_new(&locale_en.into(), options).unwrap();
collator
}

#[cfg(test)]
mod tests {
use super::*;
use serde_json::json;

#[test]
fn formats_bugs_into_github_shorthand() {
assert_eq!(
get_formatted_bugs(&PackageJson::from_value(json!({
"name": "a",
"bugs": {
"url": "https://github.com/User/repo/issues"
}
}))),
Some(json!("https://github.com/User/repo/issues"))
);
}

#[test]
fn formats_repository_into_gitlab_shorthand() {
assert_eq!(
get_formatted_repository(&PackageJson::from_value(json!({
"name": "a",
"repository": {
"url": "git://gitlab.com/User/repo",
"type": "git",
},
}))),
Some(json!("git://gitlab.com/User/repo"))
);
}

#[test]
fn formats_repository_into_github_shorthand() {
assert_eq!(
get_formatted_repository(&PackageJson::from_value(json!({
"name": "a",
"repository": {
"url": "git://github.com/User/repo",
"type": "git",
},
}))),
Some(json!("User/repo"))
);
}

#[test]
fn retains_long_format_when_directory_property_used() {
assert_eq!(
get_formatted_repository(&PackageJson::from_value(json!({
"name": "a",
"repository": {
"url": "git://gitlab.com/User/repo",
"type": "git",
"directory": "packages/foo",
},
}))),
None
);
}

#[test]
fn sorts_conditional_exports() {
assert_eq!(
get_sorted_exports(
&Rcfile::new(),
&PackageJson::from_value(json!({
"name": "a",
"exports": {
"require": "./index-require.cjs",
"import": "./index-module.js",
},
}))
),
Some(json!({
"import": "./index-module.js",
"require": "./index-require.cjs",
})),
)
}

#[test]
fn returns_none_when_conditional_exports_already_sorted() {
assert_eq!(
get_sorted_exports(
&Rcfile::new(),
&PackageJson::from_value(json!({
"name": "a",
"exports": {
"import": "./index-module.js",
"require": "./index-require.cjs",
},
}))
),
None
)
}

#[test]
fn sorts_conditional_exports_sub_paths() {
assert_eq!(
get_sorted_exports(
&Rcfile::new(),
&PackageJson::from_value(json!({
"name": "a",
"exports": {
".": "./index.js",
"./feature.js": {
"default": "./feature.js",
"node": "./feature-node.js",
},
},
}))
),
Some(json!({
".": "./index.js",
"./feature.js": {
"node": "./feature-node.js",
"default": "./feature.js",
},
})),
)
}

#[test]
fn returns_none_when_conditional_exports_sub_paths_already_sorted() {
assert_eq!(
get_sorted_exports(
&Rcfile::new(),
&PackageJson::from_value(json!({
"name": "a",
"exports": {
".": "./index.js",
"./feature.js": {
"node": "./feature-node.js",
"default": "./feature.js",
},
},
}))
),
None
)
}

#[test]
fn sorts_object_properties_alphabetically_by_key() {
assert_eq!(
get_sorted_az(
"dependencies",
&PackageJson::from_value(json!({
"dependencies": {
"B": "",
"@B": "",
"1B": "",
"A": "",
"@A": "",
"1A": "",
},
}))
),
Some(json!({
"@A": "",
"@B": "",
"1A": "",
"1B": "",
"A": "",
"B": "",
}))
);
}
#[test]
fn sorts_array_members_alphabetically_by_value() {
assert_eq!(
get_sorted_az(
"keywords",
&PackageJson::from_value(json!({
"keywords": ["B", "@B", "1B", "A", "@A", "1A"],
}))
),
Some(json!(["@A", "@B", "1A", "1B", "A", "B"]))
);
}

#[test]
fn sorts_named_root_properties_first_leaving_the_rest_alone() {
assert_eq!(
get_sorted_first(
&Rcfile::from_mock(json!({
"sortFirst": ["name", "F", "E", "D"],
"sortPackages": false,
})),
&PackageJson::from_value(json!({
"D": "",
"B": "",
"name": "a",
"F": "",
"A": "",
"E": "",
}))
),
Some(json!({
"name": "a",
"F": "",
"E": "",
"D": "",
"B": "",
"A": "",
}))
);
}

#[test]
fn sorts_all_root_properties_alphabetically() {
assert_eq!(
get_sorted_first(
&Rcfile::from_mock(json!({
"sortFirst": [],
"sortPackages": true,
})),
&PackageJson::from_value(json!({
"D": "",
"B": "",
"name": "a",
"F": "",
"A": "",
"E": "",
}))
),
Some(json!({
"A": "",
"B": "",
"D": "",
"E": "",
"F": "",
"name": "a",
}))
);
}

#[test]
fn sorts_named_properties_first_then_the_rest_alphabetically() {
assert_eq!(
get_sorted_first(
&Rcfile::from_mock(json!({
"sortFirst": ["name", "F", "E", "D"],
"sortPackages": true,
})),
&PackageJson::from_value(json!({
"name": "a",
"A": "",
"F": "",
"B": "",
"D": "",
"E": "",
}))
),
Some(json!({
"name": "a",
"F": "",
"E": "",
"D": "",
"A": "",
"B": "",
}))
);
}
}
Loading

0 comments on commit 0da933c

Please sign in to comment.