Skip to content
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
2 changes: 1 addition & 1 deletion src/commands/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ pub async fn run(cmd: &mut InitArgs) -> Result<bool> {
};

// Write the manifest to `typst.toml`.
write_manifest(&manif)?;
write_manifest(&manifest, &manif)?;

utpm_log!(info, "File created to {typ}");
Ok(true)
Expand Down
7 changes: 6 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::process::ExitCode;

use anyhow::Result;
use clap::Parser;
use tracing::{error, instrument, level_filters::LevelFilter};
Expand All @@ -20,7 +22,7 @@ use utpm::{
/// sets up logging, and dispatches to the appropriate command handler.
#[instrument]
#[tokio::main]
async fn main() {
async fn main() -> ExitCode {
// Parse command-line arguments.
ARGS.set(Cli::parse()).expect("ARGS should still be empty");
let x = get_args();
Expand Down Expand Up @@ -86,6 +88,9 @@ async fn main() {
Ok(_) => (),
Err(err2) => error!("{err2}"), // If utpm_log errors, it will fallback to that
};
ExitCode::FAILURE
} else {
ExitCode::SUCCESS
}
}

Expand Down
7 changes: 4 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,21 +115,22 @@ pub fn regex_import() -> Regex {
.unwrap()
}

/// Writes a `PackageManifest` to `./typst.toml` in pretty TOML format.
/// Writes a `PackageManifest` to the given path in pretty TOML format.
///
/// Respects dry-run mode - if dry-run is enabled, the file is not actually written.
///
/// # Arguments
/// * `path` - The path of the manifest file to write
/// * `data` - The package manifest to write
///
/// # Errors
/// Returns an error if:
/// - The manifest cannot be serialized to TOML
/// - The file cannot be written (if not in dry-run mode)
pub fn write_manifest(data: &PackageManifest) -> Result<()> {
pub fn write_manifest(path: impl AsRef<Path>, data: &PackageManifest) -> Result<()> {
let tomlfy: String = toml::to_string_pretty(data)?;
if !crate::utils::dryrun::get_dry_run() {
std::fs::write(Path::new("./typst.toml"), tomlfy)?;
std::fs::write(path, tomlfy)?;
}
Ok(())
}
Expand Down
20 changes: 20 additions & 0 deletions tests/e2e/harness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@ impl Sandbox {
pub fn utpm(&self, args: &[&str]) -> Run {
Run::new(self.cmd(args))
}

/// Runs `utpm` like [`Sandbox::utpm`], after letting `cb` adjust the command, e.g. to set
/// environment variables.
pub fn utpm_with(&self, args: &[&str], cb: impl FnOnce(&mut Command)) -> Run {
let mut cmd = self.cmd(args);
cb(&mut cmd);
Run::new(cmd)
}
}

/// The captured result of one `utpm` invocation.
Expand Down Expand Up @@ -138,6 +146,18 @@ impl Run {
self
}

/// Asserts that the process exited with a failure status.
#[track_caller]
pub fn failure(&self) -> &Self {
assert!(
!self.output.status.success(),
"`utpm {}` unexpectedly succeeded\n{}",
self.args,
self.dump(),
);
self
}

fn dump(&self) -> String {
format!(
"--- stdout\n{}\n--- stderr\n{}",
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e/init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,3 +53,17 @@ fn init_force_overwrites_existing_manifest() {

assert_eq!(read_manifest(&sb).package.name, "my-pkg");
}

#[test]
fn init_writes_manifest_to_utpm_current_dir() {
let sb = Sandbox::new();
let target = sb.project().join("sub");
std::fs::create_dir(&target).unwrap();

sb.utpm_with(&["prj", "init", "--cli", "--name", "my-pkg"], |cmd| {
cmd.env("UTPM_CURRENT_DIR", &target);
})
.success();

assert_eq!(files_in(sb.project()), ["sub/typst.toml"]);
}
12 changes: 11 additions & 1 deletion tests/e2e/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ fn link_without_force_keeps_existing_package() {
sb.utpm(&["prj", "link"]).success();

sb.write("main.typ", "new");
sb.utpm(&["prj", "link"]);
sb.utpm(&["prj", "link"]).failure();

let linked = sb.packages().join("local/my-pkg/0.1.0");
assert_eq!(
Expand Down Expand Up @@ -100,3 +100,13 @@ fn link_dry_run_writes_nothing() {

assert!(files_in(sb.packages()).is_empty());
}

#[test]
fn link_without_manifest_fails() {
let sb = Sandbox::new();
sb.write("main.typ", "");

sb.utpm(&["prj", "link"]).failure();

assert!(files_in(sb.packages()).is_empty());
}
Loading