Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: improve test coverage #23

Merged
merged 5 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 1 addition & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,12 @@ jobs:
save-cache: ${{ github.ref_name == 'main' }}

- name: Install cargo-nextest
if: runner.os != 'Windows'
uses: taiki-e/install-action@cargo-nextest

- name: Cargo check
run: cargo ck

- name: Build cache by Cargo Check and Cargo Test
if: runner.os == 'Windows'
run: |
cargo test run --workspace --all-targets --all-features --no-run

- name: Build cache by Cargo Check and Cargo Test
if: runner.os != 'Windows'
run: |
cargo nextest run --workspace --all-targets --all-features --no-run

Expand Down Expand Up @@ -163,8 +156,4 @@ jobs:
- name: Install cargo-nextest
uses: taiki-e/install-action@cargo-nextest

- if: runner.os == 'Windows'
run: cargo test

- if: runner.os != 'Windows'
run: cargo nextest run
- run: cargo nextest run
73 changes: 73 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# Run cargo-llvm-cov and upload to codecov.io

name: Code Coverage

on:
workflow_dispatch:
pull_request:
types: [opened, synchronize]
paths:
- '**.rs'
push:
branches:
- main
paths:
- '**.rs'

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: ${{ github.ref_name != 'main' }}

jobs:
coverage:
name: Code Coverage
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
submodules: true # Pull submodules for `cargo coverage`

- name: Install Rust Toolchain
uses: ./.github/actions/rustup

- name: Install cargo-llvm-cov
uses: taiki-e/install-action@cargo-llvm-cov

- name: Install cargo-nextest
uses: taiki-e/install-action@cargo-nextest

- name: Install llvm-tools-preview for llvm-cov
run: rustup component add llvm-tools-preview

- name: Run
run: cargo codecov --lcov --output-path lcov.info

- name: Upload Artifact
uses: actions/upload-artifact@v3
with:
name: codecov
path: lcov.info

# codecov often fails, use another workflow for retry
upload-codecov:
name: Upload coverage file
runs-on: ubuntu-latest
needs: coverage
# Check if the event is not triggered by a fork
if: github.event.pull_request.head.repo.full_name == github.repository
steps:
- name: Checkout
uses: actions/checkout@v3

- name: Download coverage file
uses: actions/download-artifact@v3
with:
name: codecov

- name: Upload to codecov.io
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
fail_ci_if_error: true
files: lcov.info
17 changes: 5 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ serde_json = { version = "1.0.99" }
serde_yaml = { version = "0.9.1" }
ssri = { version = "9.0.0" }
tar = { version = "0.4.38" }
tempfile = { version = "3.6.0" }
thiserror = { version = "1.0.40" }
tracing = { version = "0.1.37" }
tracing-subscriber = { version = "0.3.17", features = ["env-filter"] }
tokio = { version = "1", features = ["rt", "macros", "rt-multi-thread"] }
libdeflater = { version = "0.14.0" }
uuid = { version = "1.4.0", features = ["v4", "fast-rng"] }

[workspace.metadata.workspaces]
allow_branch = "main"
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,4 @@ tracing-subscriber = { workspace = true }
tokio = { workspace = true }

[dev-dependencies]
uuid = { workspace = true }
tempfile = { workspace = true }
25 changes: 9 additions & 16 deletions crates/cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,41 +47,34 @@ async fn run_commands(cli: Cli) -> Result<()> {

#[cfg(test)]
mod tests {
use std::{fs, io::Write, path::PathBuf};
use std::{fs, io::Write};

use uuid::Uuid;
use tempfile::tempdir;

use super::*;

fn prepare() -> PathBuf {
let parent_folder = env::temp_dir().join(Uuid::new_v4().to_string());
fs::create_dir_all(&parent_folder).unwrap();
env::set_current_dir(&parent_folder).unwrap();
parent_folder
}

#[tokio::test]
async fn init_command_should_create_package_json() {
let parent_folder = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
let parent_folder = prepare();
env::set_current_dir(parent_folder.path()).unwrap();
let cli = Cli::parse_from(["", "init"]);
run_commands(cli).await.unwrap();

assert!(parent_folder.join("package.json").exists());
assert!(parent_folder.path().join("package.json").exists());
env::set_current_dir(&current_directory).unwrap();
fs::remove_dir_all(&parent_folder).unwrap();
}

#[tokio::test]
async fn init_command_should_throw_on_existing_file() {
let parent_folder = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
let parent_folder = prepare();
let mut file = fs::File::create(parent_folder.join("package.json")).unwrap();
env::set_current_dir(&parent_folder).unwrap();
let mut file = fs::File::create(parent_folder.path().join("package.json")).unwrap();
file.write_all("{}".as_bytes()).unwrap();
assert!(parent_folder.join("package.json").exists());
assert!(parent_folder.path().join("package.json").exists());
let cli = Cli::parse_from(["", "init"]);
run_commands(cli).await.expect_err("should have thrown");
env::set_current_dir(&current_directory).unwrap();
fs::remove_dir_all(&parent_folder).unwrap();
}
}
3 changes: 3 additions & 0 deletions crates/npmrc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ repository.workspace = true
home = { workspace = true }
serde = { workspace = true }
serde_ini = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
45 changes: 41 additions & 4 deletions crates/npmrc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ pub enum PackageImportMethod {
CloneOrCopy,
}

#[derive(Debug, Deserialize, Default)]
#[derive(Debug, Deserialize)]
pub struct Npmrc {
/// When true, all dependencies are hoisted to node_modules/.pnpm/node_modules.
/// This makes unlisted dependencies accessible to all packages inside node_modules.
Expand Down Expand Up @@ -151,6 +151,12 @@ impl Npmrc {
}
}

impl Default for Npmrc {
fn default() -> Self {
Self::new()
}
}

pub fn get_current_npmrc() -> Npmrc {
// Look for current folder `.npmrc` and if not found, look for home directory.
let path = match env::current_dir() {
Expand All @@ -171,7 +177,9 @@ pub fn get_current_npmrc() -> Npmrc {

#[cfg(test)]
mod tests {
use std::{env, str::FromStr};
use std::{env, io::Write, str::FromStr};

use tempfile::tempdir;

use super::*;

Expand Down Expand Up @@ -241,14 +249,43 @@ mod tests {
}

#[test]
#[cfg(not(target_os = "windows"))]
pub fn should_use_absolute_virtual_store_dir() {
let value: Npmrc = serde_ini::from_str("virtual-store-dir=/node_modules/.pacquet").unwrap();
assert_eq!(value.virtual_store_dir, PathBuf::from_str("/node_modules/.pacquet").unwrap());
}

#[test]
pub fn add_slash_to_registry_end() {
let value: Npmrc = serde_ini::from_str("registry=https://yagiz.co").unwrap();
assert_eq!(value.registry, "https://yagiz.co/");
let without_slash: Npmrc = serde_ini::from_str("registry=https://yagiz.co").unwrap();
assert_eq!(without_slash.registry, "https://yagiz.co/");

let without_slash: Npmrc = serde_ini::from_str("registry=https://yagiz.co/").unwrap();
assert_eq!(without_slash.registry, "https://yagiz.co/");
}

#[test]
pub fn test_current_folder_for_npmrc() {
let tmp = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
let mut f = fs::File::create(tmp.path().join(".npmrc")).expect("Unable to create file");
f.write_all(b"symlink=false").unwrap();
env::set_current_dir(tmp.path()).unwrap();
let config = get_current_npmrc();
assert!(!config.symlink);
env::set_current_dir(current_directory).unwrap();
}

#[test]
pub fn test_current_folder_for_invalid_npmrc() {
let tmp = tempdir().unwrap();
let current_directory = env::current_dir().unwrap();
let mut f = fs::File::create(tmp.path().join(".npmrc")).expect("Unable to create file");
// write invalid utf-8 value to npmrc
f.write_all(b"Hello \xff World").unwrap();
env::set_current_dir(tmp.path()).unwrap();
let config = get_current_npmrc();
assert!(config.symlink);
env::set_current_dir(current_directory).unwrap();
}
}
3 changes: 3 additions & 0 deletions crates/package_json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,6 @@ repository.workspace = true
thiserror = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }

[dev-dependencies]
tempfile = { workspace = true }
Loading