Skip to content

Commit f51b777

Browse files
authored
Add tempfile as a dev-dependency and implement tests for architecture and virtual environment functionality (#309)
1 parent d01fe38 commit f51b777

File tree

8 files changed

+571
-0
lines changed

8 files changed

+571
-0
lines changed

Cargo.lock

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/pet-core/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,6 @@ lazy_static = "1.4.0"
1515
regex = "1.10.4"
1616
log = "0.4.21"
1717
serde_json = "1.0.93"
18+
19+
[dev-dependencies]
20+
tempfile = "3.10"

crates/pet-core/src/arch.rs

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,81 @@ impl std::fmt::Display for Architecture {
3636
Ok(())
3737
}
3838
}
39+
40+
#[cfg(test)]
41+
mod tests {
42+
use super::*;
43+
44+
#[test]
45+
fn test_architecture_display_x64() {
46+
let arch = Architecture::X64;
47+
assert_eq!(format!("{}", arch), "x64");
48+
}
49+
50+
#[test]
51+
fn test_architecture_display_x86() {
52+
let arch = Architecture::X86;
53+
assert_eq!(format!("{}", arch), "x86");
54+
}
55+
56+
#[test]
57+
fn test_architecture_ordering() {
58+
let x64 = Architecture::X64;
59+
let x86 = Architecture::X86;
60+
// X64 < X86 alphabetically
61+
assert!(x64 < x86);
62+
assert!(x86 > x64);
63+
assert_eq!(x64.cmp(&x64), std::cmp::Ordering::Equal);
64+
}
65+
66+
#[test]
67+
fn test_architecture_partial_ordering() {
68+
let x64 = Architecture::X64;
69+
let x86 = Architecture::X86;
70+
assert_eq!(x64.partial_cmp(&x86), Some(std::cmp::Ordering::Less));
71+
assert_eq!(x86.partial_cmp(&x64), Some(std::cmp::Ordering::Greater));
72+
assert_eq!(x64.partial_cmp(&x64), Some(std::cmp::Ordering::Equal));
73+
}
74+
75+
#[test]
76+
fn test_architecture_equality() {
77+
assert_eq!(Architecture::X64, Architecture::X64);
78+
assert_eq!(Architecture::X86, Architecture::X86);
79+
assert_ne!(Architecture::X64, Architecture::X86);
80+
}
81+
82+
#[test]
83+
fn test_architecture_clone() {
84+
let arch = Architecture::X64;
85+
let cloned = arch.clone();
86+
assert_eq!(arch, cloned);
87+
}
88+
89+
#[test]
90+
fn test_architecture_debug() {
91+
let arch = Architecture::X64;
92+
assert_eq!(format!("{:?}", arch), "X64");
93+
let arch = Architecture::X86;
94+
assert_eq!(format!("{:?}", arch), "X86");
95+
}
96+
97+
#[test]
98+
fn test_architecture_serialize() {
99+
let arch = Architecture::X64;
100+
let json = serde_json::to_string(&arch).unwrap();
101+
assert_eq!(json, "\"x64\"");
102+
103+
let arch = Architecture::X86;
104+
let json = serde_json::to_string(&arch).unwrap();
105+
assert_eq!(json, "\"x86\"");
106+
}
107+
108+
#[test]
109+
fn test_architecture_deserialize() {
110+
let arch: Architecture = serde_json::from_str("\"x64\"").unwrap();
111+
assert_eq!(arch, Architecture::X64);
112+
113+
let arch: Architecture = serde_json::from_str("\"x86\"").unwrap();
114+
assert_eq!(arch, Architecture::X86);
115+
}
116+
}

crates/pet-core/src/pyvenv_cfg.rs

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,3 +186,150 @@ fn parse_prompt(line: &str) -> Option<String> {
186186
}
187187
None
188188
}
189+
190+
#[cfg(test)]
191+
mod tests {
192+
use super::*;
193+
use std::io::Write;
194+
use tempfile::tempdir;
195+
196+
#[test]
197+
fn test_parse_version_standard() {
198+
let line = "version = 3.11.4";
199+
let result = parse_version(line, &VERSION);
200+
assert!(result.is_some());
201+
let (ver, major, minor) = result.unwrap();
202+
assert_eq!(ver, "3.11.4");
203+
assert_eq!(major, 3);
204+
assert_eq!(minor, 11);
205+
}
206+
207+
#[test]
208+
fn test_parse_version_info() {
209+
let line = "version_info = 3.12.0.final";
210+
let result = parse_version(line, &VERSION_INFO);
211+
assert!(result.is_some());
212+
let (ver, major, minor) = result.unwrap();
213+
assert_eq!(ver, "3.12.0.final");
214+
assert_eq!(major, 3);
215+
assert_eq!(minor, 12);
216+
}
217+
218+
#[test]
219+
fn test_parse_version_no_match() {
220+
let line = "home = /usr/bin/python";
221+
let result = parse_version(line, &VERSION);
222+
assert!(result.is_none());
223+
}
224+
225+
#[test]
226+
fn test_parse_prompt_double_quotes() {
227+
let line = r#"prompt = "my-env""#;
228+
let result = parse_prompt(line);
229+
assert_eq!(result, Some("my-env".to_string()));
230+
}
231+
232+
#[test]
233+
fn test_parse_prompt_single_quotes() {
234+
let line = "prompt = 'my-env'";
235+
let result = parse_prompt(line);
236+
assert_eq!(result, Some("my-env".to_string()));
237+
}
238+
239+
#[test]
240+
fn test_parse_prompt_no_quotes() {
241+
let line = "prompt = my-venv";
242+
let result = parse_prompt(line);
243+
assert_eq!(result, Some("my-venv".to_string()));
244+
}
245+
246+
#[test]
247+
fn test_parse_prompt_with_spaces() {
248+
let line = "prompt = my-venv ";
249+
let result = parse_prompt(line);
250+
assert_eq!(result, Some("my-venv".to_string()));
251+
}
252+
253+
#[test]
254+
fn test_parse_prompt_empty_value() {
255+
let line = "prompt = ";
256+
let result = parse_prompt(line);
257+
assert!(result.is_none());
258+
}
259+
260+
#[test]
261+
fn test_parse_prompt_not_prompt_line() {
262+
let line = "home = /usr/bin/python";
263+
let result = parse_prompt(line);
264+
assert!(result.is_none());
265+
}
266+
267+
#[test]
268+
fn test_pyvenv_cfg_find_in_directory() {
269+
let dir = tempdir().unwrap();
270+
let cfg_path = dir.path().join("pyvenv.cfg");
271+
let mut file = fs::File::create(&cfg_path).unwrap();
272+
writeln!(file, "version = 3.11.4").unwrap();
273+
writeln!(file, "prompt = test-env").unwrap();
274+
275+
let result = PyVenvCfg::find(dir.path());
276+
assert!(result.is_some());
277+
let cfg = result.unwrap();
278+
assert_eq!(cfg.version, "3.11.4");
279+
assert_eq!(cfg.version_major, 3);
280+
assert_eq!(cfg.version_minor, 11);
281+
assert_eq!(cfg.prompt, Some("test-env".to_string()));
282+
}
283+
284+
#[test]
285+
fn test_pyvenv_cfg_find_from_bin() {
286+
let dir = tempdir().unwrap();
287+
let bin_dir = dir.path().join("bin");
288+
fs::create_dir_all(&bin_dir).unwrap();
289+
290+
let cfg_path = dir.path().join("pyvenv.cfg");
291+
let mut file = fs::File::create(&cfg_path).unwrap();
292+
writeln!(file, "version = 3.10.0").unwrap();
293+
294+
let result = PyVenvCfg::find(&bin_dir);
295+
assert!(result.is_some());
296+
let cfg = result.unwrap();
297+
assert_eq!(cfg.version, "3.10.0");
298+
assert_eq!(cfg.version_major, 3);
299+
assert_eq!(cfg.version_minor, 10);
300+
}
301+
302+
#[test]
303+
fn test_pyvenv_cfg_not_found() {
304+
let dir = tempdir().unwrap();
305+
let result = PyVenvCfg::find(dir.path());
306+
assert!(result.is_none());
307+
}
308+
309+
#[test]
310+
fn test_pyvenv_cfg_missing_version() {
311+
let dir = tempdir().unwrap();
312+
let cfg_path = dir.path().join("pyvenv.cfg");
313+
let mut file = fs::File::create(&cfg_path).unwrap();
314+
writeln!(file, "home = /usr/bin/python").unwrap();
315+
writeln!(file, "prompt = my-env").unwrap();
316+
317+
let result = PyVenvCfg::find(dir.path());
318+
assert!(result.is_none()); // Version is required
319+
}
320+
321+
#[test]
322+
fn test_pyvenv_cfg_version_info_format() {
323+
let dir = tempdir().unwrap();
324+
let cfg_path = dir.path().join("pyvenv.cfg");
325+
let mut file = fs::File::create(&cfg_path).unwrap();
326+
writeln!(file, "version_info = 3.12.1.final.0").unwrap();
327+
328+
let result = PyVenvCfg::find(dir.path());
329+
assert!(result.is_some());
330+
let cfg = result.unwrap();
331+
assert_eq!(cfg.version, "3.12.1.final.0");
332+
assert_eq!(cfg.version_major, 3);
333+
assert_eq!(cfg.version_minor, 12);
334+
}
335+
}

crates/pet-venv/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,6 @@ pet-core = { path = "../pet-core" }
1212
pet-virtualenv = { path = "../pet-virtualenv" }
1313
pet-python-utils = { path = "../pet-python-utils" }
1414
log = "0.4.21"
15+
16+
[dev-dependencies]
17+
tempfile = "3.10"

0 commit comments

Comments
 (0)