Skip to content

Commit

Permalink
Add tests for the wit tool.
Browse files Browse the repository at this point in the history
  • Loading branch information
peterhuene committed Jul 28, 2023
1 parent 98f3b15 commit dc378c2
Show file tree
Hide file tree
Showing 23 changed files with 944 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Build cargo-component
run: cargo build
- name: Test cargo-component
run: cargo test
run: cargo test --all

example:
name: Build example component
Expand Down
5 changes: 5 additions & 0 deletions Cargo.lock

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

7 changes: 7 additions & 0 deletions crates/wit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,10 @@ futures = { workspace = true }
bytes = { workspace = true }
tokio = { workspace = true }
pretty_env_logger = { workspace = true }

[dev-dependencies]
assert_cmd = "2.0.12"
predicates = "3.0.3"
warg-server = { git = "https://github.com/bytecodealliance/registry" }
tokio-util = { workspace = true }
wasmparser = "0.110.0"
38 changes: 23 additions & 15 deletions crates/wit/src/commands/add.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
use super::CommonOptions;
use crate::config::{Config, CONFIG_FILE_NAME};
use crate::{
config::{Config, CONFIG_FILE_NAME},
resolve_dependencies,
};
use anyhow::{bail, Context, Result};
use cargo_component_core::{
registry::{Dependency, DependencyResolution, DependencyResolver, RegistryPackage},
terminal::{Color, Terminal},
terminal::Terminal,
VersionedPackageId,
};
use clap::Args;
Expand Down Expand Up @@ -52,10 +55,6 @@ pub struct AddCommand {
#[clap(flatten)]
pub common: CommonOptions,

/// Coloring: auto, always, never
#[clap(long = "color", value_name = "WHEN")]
pub color: Option<Color>,

/// Don't actually write the configuration file.
#[clap(long = "dry-run")]
pub dry_run: bool,
Expand All @@ -81,13 +80,13 @@ impl AddCommand {
let (mut config, config_path) = Config::from_default_file()?
.with_context(|| format!("failed to find configuration file `{CONFIG_FILE_NAME}`"))?;

let warg_config = warg_client::Config::from_default_file()?.unwrap_or_default();

let id = self.id.as_ref().unwrap_or(&self.package.id);
if config.dependencies.contains_key(id) {
bail!("cannot add dependency `{id}` as it conflicts with an existing dependency");
}

let warg_config = warg_client::Config::from_default_file()?.unwrap_or_default();

let terminal = self.common.new_terminal();
let version = resolve_version(
&config,
Expand All @@ -98,19 +97,28 @@ impl AddCommand {
)
.await?;

let package = match &self.id {
Some(id) => RegistryPackage {
id: Some(id.clone()),
version: version.parse()?,
registry: self.registry,
},
None => version.parse()?,
let package = RegistryPackage {
id: self.id.is_some().then(|| self.package.id.clone()),
version: version.parse().expect("expected a valid version"),
registry: self.registry,
};

let id = self.id.as_ref().unwrap_or(&self.package.id);

config
.dependencies
.insert(id.clone(), Dependency::Package(package));

// Resolve all dependencies to ensure that the lockfile is up to date.
resolve_dependencies(
&config,
&config_path,
&warg_config,
&terminal,
!self.dry_run,
)
.await?;

if !self.dry_run {
config.write(config_path)?;
}
Expand Down
1 change: 1 addition & 0 deletions crates/wit/src/commands/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use std::{fs, path::PathBuf};

/// Build a binary WIT package.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct BuildCommand {
/// The common command options.
#[clap(flatten)]
Expand Down
1 change: 1 addition & 0 deletions crates/wit/src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use url::Url;

/// Initialize a new WIT package.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct InitCommand {
/// The common command options.
#[clap(flatten)]
Expand Down
4 changes: 4 additions & 0 deletions crates/wit/src/commands/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use warg_crypto::signing::PrivateKey;

/// Manage signing keys for publishing packages to a registry.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct KeyCommand {
/// The common command options.
#[clap(flatten)]
Expand Down Expand Up @@ -49,6 +50,7 @@ pub enum KeySubcommand {

/// Creates a new signing key for a registry in the local keyring.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct KeyNewCommand {
/// The key name to use for the signing key.
#[clap(long, short, value_name = "NAME", default_value = "default")]
Expand Down Expand Up @@ -101,6 +103,7 @@ impl KeyNewCommand {

/// Sets the signing key for a registry in the local keyring.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct KeySetCommand {
/// The key name to use for the signing key.
#[clap(long, short, value_name = "NAME", default_value = "default")]
Expand Down Expand Up @@ -136,6 +139,7 @@ impl KeySetCommand {

/// Deletes the signing key for a registry from the local keyring.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct KeyDeleteCommand {
/// The key name to use for the signing key.
#[clap(long, short, value_name = "NAME", default_value = "default")]
Expand Down
1 change: 1 addition & 0 deletions crates/wit/src/commands/publish.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use warg_protocol::registry::PackageId;

/// Publish a WIT package to a registry.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct PublishCommand {
/// The common command options.
#[clap(flatten)]
Expand Down
1 change: 1 addition & 0 deletions crates/wit/src/commands/update.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use clap::Args;

/// Update dependencies as recorded in the lock file.
#[derive(Args)]
#[clap(disable_version_flag = true)]
pub struct UpdateCommand {
/// The common command options.
#[clap(flatten)]
Expand Down
34 changes: 19 additions & 15 deletions crates/wit/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ async fn resolve_dependencies(
config_path: &Path,
warg_config: &warg_client::Config,
terminal: &Terminal,
update_lock_file: bool,
) -> Result<DependencyResolutionMap> {
let file_lock = acquire_lock_file_ro(terminal, config_path)?;
let lock_file = file_lock
Expand Down Expand Up @@ -62,18 +63,20 @@ async fn resolve_dependencies(
let map = resolver.resolve().await?;

// Update the lock file
let new_lock_file = to_lock_file(&map);
if Some(&new_lock_file) != lock_file.as_ref() {
drop(file_lock);
let file_lock = acquire_lock_file_rw(terminal, config_path)?;
new_lock_file
.write(file_lock.file(), "wit")
.with_context(|| {
format!(
"failed to write lock file `{path}`",
path = file_lock.path().display()
)
})?;
if update_lock_file {
let new_lock_file = to_lock_file(&map);
if Some(&new_lock_file) != lock_file.as_ref() {
drop(file_lock);
let file_lock = acquire_lock_file_rw(terminal, config_path)?;
new_lock_file
.write(file_lock.file(), "wit")
.with_context(|| {
format!(
"failed to write lock file `{path}`",
path = file_lock.path().display()
)
})?;
}
}

Ok(map)
Expand Down Expand Up @@ -277,7 +280,8 @@ async fn build_wit_package(
warg_config: &warg_client::Config,
terminal: &Terminal,
) -> Result<(PackageId, Vec<u8>)> {
let dependencies = resolve_dependencies(config, config_path, warg_config, terminal).await?;
let dependencies =
resolve_dependencies(config, config_path, warg_config, terminal, true).await?;

let dir = config_path.parent().unwrap_or_else(|| Path::new("."));

Expand Down Expand Up @@ -431,9 +435,9 @@ pub async fn update_lockfile(
let new_ver = &new_pkg.versions[new_ver_index];
if old_ver.version != new_ver.version {
terminal.status_with_color(
"Updating",
if dry_run { "Would update" } else { "Updating" },
format!(
"component registry package `{id}` v{old} -> v{new}",
"dependency `{id}` v{old} -> v{new}",
id = old_pkg.id,
old = old_ver.version,
new = new_ver.version
Expand Down
Loading

0 comments on commit dc378c2

Please sign in to comment.