feat: add snapshot tests for CLI

This commit is contained in:
Harsh Shandilya 2024-02-28 10:14:50 +05:30
parent 024d7a6c37
commit 45e8fad9cb
9 changed files with 189 additions and 12 deletions

71
Cargo.lock generated
View File

@ -24,6 +24,7 @@ dependencies = [
"clap",
"color-eyre",
"futures",
"insta-cmd",
"reqwest",
"roxmltree",
"semver",
@ -195,6 +196,18 @@ version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "console"
version = "0.15.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb"
dependencies = [
"encode_unicode",
"lazy_static",
"libc",
"windows-sys 0.52.0",
]
[[package]]
name = "core-foundation"
version = "0.9.4"
@ -211,6 +224,12 @@ version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ea2b9bc92be3c2baa9334a323ebca2d6f074ff852cd1d7b11064035cd3868f"
[[package]]
name = "encode_unicode"
version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f"
[[package]]
name = "encoding_rs"
version = "0.8.33"
@ -492,6 +511,31 @@ dependencies = [
"hashbrown",
]
[[package]]
name = "insta"
version = "1.35.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7c985c1bef99cf13c58fade470483d81a2bfe846ebde60ed28cc2dddec2df9e2"
dependencies = [
"console",
"lazy_static",
"linked-hash-map",
"serde",
"similar",
"yaml-rust",
]
[[package]]
name = "insta-cmd"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1980f17994b79f75670aa90cfc8d35edc4aa248f16aa48b5e27835b080e452a2"
dependencies = [
"insta",
"serde",
"serde_json",
]
[[package]]
name = "ipnet"
version = "2.9.0"
@ -513,12 +557,24 @@ dependencies = [
"wasm-bindgen",
]
[[package]]
name = "lazy_static"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "linked-hash-map"
version = "0.5.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0717cef1bc8b636c6e1c1bbdefc09e6322da8a9321966e8928ef80d20f7f770f"
[[package]]
name = "log"
version = "0.4.21"
@ -788,6 +844,12 @@ dependencies = [
"serde",
]
[[package]]
name = "similar"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32fea41aca09ee824cc9724996433064c89f7777e60762749a4170a14abbfa21"
[[package]]
name = "slab"
version = "0.4.9"
@ -1257,3 +1319,12 @@ dependencies = [
"cfg-if",
"windows-sys 0.48.0",
]
[[package]]
name = "yaml-rust"
version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56c1936c4cc7a1c9ab21a1ebb602eb942ba868cbd44a99cb7cdc5892335e1c85"
dependencies = [
"linked-hash-map",
]

View File

@ -29,3 +29,6 @@ maintenance = { status = "actively-developed" }
[features]
measure-alloc = []
[dev-dependencies]
insta-cmd = "0.5.0"

View File

@ -55,9 +55,34 @@ async fn main() -> Result<()> {
#[cfg(test)]
mod test {
use super::Cli;
use insta_cmd::{assert_cmd_snapshot, get_cargo_bin};
use std::process::Command;
#[test]
fn cli_assert() {
<Cli as clap::CommandFactory>::command().debug_assert();
}
#[test]
fn cli_help() {
assert_cmd_snapshot!(Command::new(get_cargo_bin("adx")).arg("--help"));
}
#[test]
fn cli_search() {
assert_cmd_snapshot!(Command::new(get_cargo_bin("adx")).arg("appcompat"));
}
#[test]
fn cli_search_stable() {
assert_cmd_snapshot!(Command::new(get_cargo_bin("adx"))
.arg("--channel")
.arg("stable")
.arg("appcompat"));
}
#[test]
fn cli_search_no_results() {
assert_cmd_snapshot!(Command::new(get_cargo_bin("adx")).arg("qtc"));
}
}

View File

@ -10,12 +10,12 @@ use crate::channel::Channel;
use crate::package::MavenPackage;
use crate::version::Version;
#[cfg(not(test))]
#[cfg(not(any(test, nix_check)))]
const BASE_MAVEN_URL: &str = "https://dl.google.com/dl/android/maven2";
/// Downloads the Maven master index for Google's Maven Repository
/// and returns the XML as a String
#[cfg(not(test))]
#[cfg(not(any(test, nix_check)))]
async fn get_maven_index() -> Result<String> {
reqwest::get(format!("{BASE_MAVEN_URL}/master-index.xml"))
.await?
@ -24,14 +24,14 @@ async fn get_maven_index() -> Result<String> {
.map_err(|e| eyre!(e))
}
#[cfg(test)]
#[cfg(any(test, nix_check))]
#[allow(clippy::unused_async)]
async fn get_maven_index() -> Result<String> {
std::fs::read_to_string("../testdata/master-index.xml").map_err(|e| eyre!(e))
}
/// Downloads the group index for the given group.
#[cfg(not(test))]
#[cfg(not(any(test, nix_check)))]
async fn get_group_index(group: &str) -> Result<String> {
reqwest::get(format!(
"{}/{}/group-index.xml",
@ -44,7 +44,7 @@ async fn get_group_index(group: &str) -> Result<String> {
.map_err(|e| eyre!(e))
}
#[cfg(test)]
#[cfg(any(test, nix_check))]
#[allow(clippy::unused_async)]
async fn get_group_index(group: &str) -> Result<String> {
std::fs::read_to_string(format!("../testdata/{group}.xml")).map_err(|e| eyre!(e))

View File

@ -0,0 +1,23 @@
---
source: adx/src/main.rs
info:
program: adx
args:
- "--help"
---
success: true
exit_code: 0
----- stdout -----
Poll Google's Maven repository to fetch the latest versions of AndroidX packages
Usage: adx [OPTIONS] <SEARCH_TERM>
Arguments:
<SEARCH_TERM> search term to filter packages with
Options:
-c, --channel <CHANNEL> the release channel to find packages from [default: alpha] [possible values: dev, alpha, beta, rc, stable]
-h, --help Print help
-V, --version Print version
----- stderr -----

View File

@ -0,0 +1,14 @@
---
source: adx/src/main.rs
info:
program: adx
args:
- appcompat
---
success: true
exit_code: 0
----- stdout -----
androidx.appcompat:appcompat:1.4.0-alpha03
androidx.appcompat:appcompat-resources:1.4.0-alpha03
----- stderr -----

View File

@ -0,0 +1,13 @@
---
source: adx/src/main.rs
info:
program: adx
args:
- qtc
---
success: true
exit_code: 0
----- stdout -----
No results found!
----- stderr -----

View File

@ -0,0 +1,16 @@
---
source: adx/src/main.rs
info:
program: adx
args:
- "--channel"
- stable
- appcompat
---
success: true
exit_code: 0
----- stdout -----
androidx.appcompat:appcompat:1.3.1
androidx.appcompat:appcompat-resources:1.3.1
----- stderr -----

View File

@ -46,16 +46,17 @@
};
craneLib = (crane.mkLib pkgs).overrideToolchain rustStable;
xmlFilter = path: _type: builtins.match ".*xml$" path != null;
xmlOrCargo = path: type:
(xmlFilter path type) || (craneLib.filterCargoSources path type);
xmlFilter = path: builtins.match ".*xml$" path != null;
instaFilter = path: builtins.match ".*snap$" path != null;
filter = path: type:
(xmlFilter path) || (instaFilter path) || (craneLib.filterCargoSources path type);
workspaceName = craneLib.crateNameFromCargoToml {cargoToml = ./adx/Cargo.toml;};
commonArgs = {
inherit (workspaceName) pname version;
src = pkgs.lib.cleanSourceWith {
src = craneLib.path ./.;
filter = xmlOrCargo;
inherit filter;
};
buildInputs = [];
nativeBuildInputs = [];
@ -63,8 +64,17 @@
cargoToml = ./adx/Cargo.toml;
};
cargoArtifacts = craneLib.buildDepsOnly (commonArgs // {doCheck = false;});
adx = craneLib.buildPackage (commonArgs // {doCheck = false;});
cargoArtifacts = craneLib.buildDepsOnly commonArgs;
adx-build = craneLib.cargoBuild (commonArgs
// {
inherit cargoArtifacts;
CARGO_BUILD_RUSTFLAGS = "--cfg nix_check";
});
adx = craneLib.buildPackage (commonArgs
// {
cargoArtifacts = adx-build;
doCheck = false;
});
adx-clippy = craneLib.cargoClippy (commonArgs
// {
inherit cargoArtifacts;
@ -73,9 +83,10 @@
adx-audit = craneLib.cargoAudit (commonArgs // {inherit advisory-db;});
adx-nextest = craneLib.cargoNextest (commonArgs
// {
inherit cargoArtifacts;
cargoArtifacts = adx-build;
partitions = 1;
partitionType = "count";
CARGO_BUILD_RUSTFLAGS = "--cfg nix_check";
});
in {
checks = {
@ -99,6 +110,7 @@
];
packages = with pkgs; [
cargo-insta
cargo-nextest
cargo-release
rustStable