Skip to content
Open
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
7 changes: 5 additions & 2 deletions src/commands/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ fn cmd_show(output: OutputFormat, private_key_flag: Option<&str>) -> Result<()>
}

fn cmd_reset(output: OutputFormat, force: bool) -> Result<()> {
if matches!(output, OutputFormat::Json) && !force {
bail!("wallet reset in JSON mode requires --force");
}

if !config::config_exists() {
match output {
OutputFormat::Table => println!("Nothing to reset. No config found."),
Expand All @@ -238,8 +242,7 @@ fn cmd_reset(output: OutputFormat, force: bool) -> Result<()> {
let mut input = String::new();
io::stdin().lock().read_line(&mut input)?;
if !matches!(input.trim().to_lowercase().as_str(), "y" | "yes") {
println!("Aborted.");
return Ok(());
bail!("Aborted.");
}
}

Expand Down
94 changes: 94 additions & 0 deletions tests/cli_integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

use assert_cmd::Command;
use predicates::prelude::*;
use std::fs;
use std::path::{Path, PathBuf};
use std::time::{SystemTime, UNIX_EPOCH};

fn polymarket() -> Command {
let mut cmd = Command::cargo_bin("polymarket").unwrap();
Expand All @@ -10,6 +13,39 @@ fn polymarket() -> Command {
cmd
}

struct TestHome(PathBuf);

impl TestHome {
fn with_config() -> Self {
let unique = SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos();
let path = std::env::temp_dir().join(format!(
"polymarket-cli-test-{}-{unique}",
std::process::id()
));
let config_dir = path.join(".config/polymarket");
fs::create_dir_all(&config_dir).unwrap();
fs::write(config_dir.join("config.json"), "{}").unwrap();
Self(path)
}

fn path(&self) -> &Path {
&self.0
}

fn config_dir(&self) -> PathBuf {
self.0.join(".config/polymarket")
}
}

impl Drop for TestHome {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
}
}

#[test]
fn help_lists_all_top_level_commands() {
polymarket().arg("--help").assert().success().stdout(
Expand Down Expand Up @@ -177,6 +213,64 @@ fn wallet_show_json_has_configured_field() {
);
}

#[test]
fn wallet_reset_json_without_force_fails_with_one_json_result() {
let home = TestHome::with_config();
let output = polymarket()
.env("HOME", home.path())
.args(["-o", "json", "wallet", "reset"])
.write_stdin("n\n")
.output()
.unwrap();

assert!(!output.status.success());
assert!(output.stderr.is_empty());
let stdout = String::from_utf8_lossy(&output.stdout);
let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
.unwrap_or_else(|e| panic!("stdout not one valid JSON result: {e}\nstdout: {stdout}"));
assert!(
parsed["error"]
.as_str()
.is_some_and(|error| error.contains("--force")),
"unexpected result: {parsed}"
);
assert!(home.config_dir().exists());
}

#[test]
fn wallet_reset_table_decline_fails_and_preserves_config() {
let home = TestHome::with_config();

polymarket()
.env("HOME", home.path())
.args(["wallet", "reset"])
.write_stdin("n\n")
.assert()
.failure()
.stdout(predicate::str::contains("Are you sure?"))
.stderr(predicate::str::contains("Aborted"));

assert!(home.config_dir().exists());
}

#[test]
fn wallet_reset_json_force_removes_config() {
let home = TestHome::with_config();
let output = polymarket()
.env("HOME", home.path())
.args(["-o", "json", "wallet", "reset", "--force"])
.output()
.unwrap();

assert!(output.status.success());
assert!(output.stderr.is_empty());
let stdout = String::from_utf8_lossy(&output.stdout);
let parsed: serde_json::Value = serde_json::from_str(stdout.trim())
.unwrap_or_else(|e| panic!("stdout not valid JSON: {e}\nstdout: {stdout}"));
assert_eq!(parsed["reset"], true);
assert!(!home.config_dir().exists());
}

#[test]
fn tags_help_lists_subcommands() {
polymarket()
Expand Down