Skip to content

Commit ab68a9d

Browse files
committed
better tests
1 parent 4676562 commit ab68a9d

File tree

3 files changed

+102
-31
lines changed

3 files changed

+102
-31
lines changed

rust/functora-cfg/Cargo.lock

Lines changed: 83 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/functora-cfg/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@ functor_derive = "0.4.3"
1919
serde = { version = "1.0.228", features = ["derive"] }
2020
serial_test = "3.2.0"
2121
temp-env = "0.3.6"
22+
tempfile = "3.23.0"

rust/functora-cfg/tests/integration.rs

Lines changed: 18 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ use serde::{Deserialize, Serialize};
55
use serial_test::serial;
66
use std::collections::HashMap;
77
use std::fmt::Debug;
8+
use std::io::Write;
89
use temp_env::with_vars;
10+
use tempfile::NamedTempFile;
911

1012
#[derive(
1113
Eq, PartialEq, Debug, Clone, Serialize, Deserialize,
@@ -19,7 +21,7 @@ struct Cfg {
1921
}
2022

2123
impl Cfg {
22-
pub fn new(cli: Cli<SubAccounts>) -> Self {
24+
pub fn new(cli: Cli<SubAccount>) -> Self {
2325
functora_cfg::Cfg {
2426
default: &Cli::def(),
2527
file_path: |cli| cli.toml.as_deref(),
@@ -33,45 +35,32 @@ impl Cfg {
3335
}
3436

3537
#[derive(
36-
Eq,
37-
PartialEq,
38-
Ord,
39-
PartialOrd,
40-
Debug,
41-
Clone,
42-
Serialize,
43-
Deserialize,
44-
Args,
38+
Eq, PartialEq, Debug, Clone, Serialize, Deserialize,
4539
)]
4640
pub struct Account {
47-
#[arg(long)]
4841
alias: String,
49-
#[arg(long)]
5042
balance: i32,
51-
#[arg(long)]
5243
tags: Option<Vec<String>>,
5344
}
5445

5546
#[derive(
5647
Eq,
5748
PartialEq,
58-
Ord,
59-
PartialOrd,
6049
Debug,
6150
Clone,
6251
Serialize,
6352
Deserialize,
6453
Subcommand,
6554
)]
6655
#[command(subcommand_precedence_over_arg = true)]
67-
pub enum SubAccounts {
68-
SubAccounts(ReClap<Account, Self>),
56+
pub enum SubAccount {
57+
SubAccount(ReClap<CliAccount, Self>),
6958
}
7059

71-
impl SubAccounts {
72-
pub fn hash_map(self) -> HashMap<String, Account> {
73-
let SubAccounts::SubAccounts(prev) = self;
74-
prev.hash_map(|SubAccounts::SubAccounts(next)| next)
60+
impl SubAccount {
61+
pub fn hash_map(self) -> HashMap<String, CliAccount> {
62+
let SubAccount::SubAccount(prev) = self;
63+
prev.hash_map(|SubAccount::SubAccount(next)| next)
7564
}
7665
}
7766

@@ -112,8 +101,6 @@ where
112101
#[derive(
113102
Eq,
114103
PartialEq,
115-
Ord,
116-
PartialOrd,
117104
Debug,
118105
Clone,
119106
Serialize,
@@ -129,7 +116,7 @@ pub struct CliAccount {
129116
tags: Option<Vec<String>>,
130117
}
131118

132-
impl Cli<IdClap<HashMap<String, Account>>> {
119+
impl Cli<IdClap<HashMap<String, CliAccount>>> {
133120
pub fn def() -> Self {
134121
Cli {
135122
toml: None,
@@ -148,7 +135,7 @@ impl Cli<IdClap<HashMap<String, Account>>> {
148135

149136
#[test]
150137
#[serial]
151-
fn defaults_only() {
138+
fn defaults() {
152139
let lhs = Cfg::new(Cli::parse_from(["functora"]));
153140
let rhs = Cfg {
154141
host: "127.0.0.1".into(),
@@ -167,8 +154,9 @@ fn defaults_only() {
167154
#[test]
168155
#[serial]
169156
fn file_override() {
170-
let path = std::env::temp_dir().join("fun.toml");
171-
let file = r#"
157+
let mut file =
158+
NamedTempFile::with_suffix(".toml").unwrap();
159+
let text = r#"
172160
host = "192.168.1.100"
173161
logs = true
174162
@@ -177,11 +165,11 @@ fn file_override() {
177165
balance = 101
178166
tags = ["retro", "story"]
179167
"#;
180-
std::fs::write(&path, file).unwrap();
168+
file.write(text.as_bytes()).unwrap();
181169
let lhs = Cfg::new(Cli::parse_from([
182170
"functora",
183171
"--toml",
184-
&path.to_string_lossy().into_owned(),
172+
&file.path().to_string_lossy().into_owned(),
185173
]));
186174
let rhs = Cfg {
187175
host: "192.168.1.100".into(),
@@ -263,7 +251,7 @@ fn cli_override() {
263251
"6060",
264252
"--logs",
265253
"true",
266-
"sub-accounts",
254+
"sub-account",
267255
"--alias",
268256
"Cli Carol",
269257
"--balance",

0 commit comments

Comments
 (0)