Skip to content

Commit e5bec09

Browse files
committed
test: fix tests
1 parent 7178327 commit e5bec09

File tree

3 files changed

+40
-86
lines changed

3 files changed

+40
-86
lines changed

src/test_utils/git.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,7 @@ impl DockerGit {
3939
}
4040

4141
pub fn run_git_command(&self, test_dir: &TestDir, args: &[&str]) -> io::Result<String> {
42-
let mut git_args = vec!["--git-dir=.git"];
43-
git_args.extend(args);
44-
45-
let git_command = git_args
42+
let git_command = args
4643
.iter()
4744
.map(|arg| {
4845
if arg.contains(' ') {
@@ -61,7 +58,7 @@ impl DockerGit {
6158
// Create initial file and setup repo in single command to avoid race conditions
6259
test_dir.create_file("README.md", "# Test Repository")?;
6360
let init_script = [
64-
"git init",
61+
"git init -b main",
6562
"git config user.name 'Test User'",
6663
"git config user.email 'test@example.com'",
6764
"git add .",
@@ -180,6 +177,7 @@ mod tests {
180177
}
181178

182179
#[test]
180+
#[ignore = "docker"]
183181
fn test_setup_initialized_repo_without_docker() {
184182
let result = std::panic::catch_unwind(|| {
185183
let (_dir, _docker_git) = setup_initialized_repo();
@@ -188,6 +186,7 @@ mod tests {
188186
}
189187

190188
#[test]
189+
#[ignore = "docker"]
191190
fn test_setup_repo_with_commit_without_docker() {
192191
let result = std::panic::catch_unwind(|| {
193192
let (_dir, _docker_git) = setup_repo_with_commit();

src/vcs/git.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ mod tests {
236236
&init_script,
237237
])
238238
.output()
239-
.unwrap();
239+
.expect("should execute docker command");
240240
assert!(
241241
output.status.success(),
242242
"Docker git setup with tag failed: {}",
@@ -265,7 +265,7 @@ mod tests {
265265
#[ignore = "docker"]
266266
fn test_is_available() {
267267
let temp_dir = setup_git_repo();
268-
let git_vcs = GitVcs::new(temp_dir.path()).unwrap();
268+
let git_vcs = GitVcs::new(temp_dir.path()).expect("should create GitVcs");
269269
assert!(git_vcs.is_available(temp_dir.path()));
270270
}
271271

@@ -282,8 +282,8 @@ mod tests {
282282
#[ignore = "docker"]
283283
fn test_get_vcs_data_with_commit() {
284284
let temp_dir = setup_git_repo_with_commit();
285-
let git_vcs = GitVcs::new(temp_dir.path()).unwrap();
286-
let data = git_vcs.get_vcs_data().unwrap();
285+
let git_vcs = GitVcs::new(temp_dir.path()).expect("should create GitVcs");
286+
let data = git_vcs.get_vcs_data().expect("should get vcs data");
287287

288288
assert!(!data.commit_hash.is_empty());
289289
assert!(!data.commit_hash_short.is_empty());
@@ -296,8 +296,8 @@ mod tests {
296296
#[ignore = "docker"]
297297
fn test_get_vcs_data_with_tag() {
298298
let temp_dir = setup_git_repo_with_tag("v1.0.0");
299-
let git_vcs = GitVcs::new(temp_dir.path()).unwrap();
300-
let data = git_vcs.get_vcs_data().unwrap();
299+
let git_vcs = GitVcs::new(temp_dir.path()).expect("should create GitVcs");
300+
let data = git_vcs.get_vcs_data().expect("should get vcs data");
301301

302302
assert!(!data.commit_hash.is_empty());
303303
assert!(!data.commit_hash_short.is_empty());
@@ -328,15 +328,15 @@ mod tests {
328328
"echo 'test content 2' > test2.txt && git add . && git commit -m 'second commit'",
329329
])
330330
.output()
331-
.unwrap();
331+
.expect("should execute docker command");
332332
assert!(
333333
output.status.success(),
334334
"Docker git second commit failed: {}",
335335
String::from_utf8_lossy(&output.stderr)
336336
);
337337

338-
let git_vcs = GitVcs::new(temp_dir.path()).unwrap();
339-
let data = git_vcs.get_vcs_data().unwrap();
338+
let git_vcs = GitVcs::new(temp_dir.path()).expect("should create GitVcs");
339+
let data = git_vcs.get_vcs_data().expect("should get vcs data");
340340

341341
assert_eq!(data.tag_version, Some("v1.0.0".to_string()));
342342
assert_eq!(data.distance, 1);
@@ -361,8 +361,8 @@ mod tests {
361361
#[ignore = "docker"]
362362
fn test_clean_working_directory() {
363363
let temp_dir = setup_git_repo();
364-
let git_vcs = GitVcs::new(temp_dir.path()).unwrap();
365-
let data = git_vcs.get_vcs_data().unwrap();
364+
let git_vcs = GitVcs::new(temp_dir.path()).expect("should create GitVcs");
365+
let data = git_vcs.get_vcs_data().expect("should get vcs data");
366366

367367
assert!(!data.is_dirty);
368368
}

src/version/zerv/utils.rs

Lines changed: 25 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,17 @@ pub fn normalize_pre_release_label(label: &str) -> Option<PreReleaseLabel> {
3131
}
3232
}
3333

34+
fn parse_timestamp_component(
35+
dt: &chrono::DateTime<chrono::Utc>,
36+
format_str: &str,
37+
component_type: &str,
38+
) -> Result<u64> {
39+
dt.format(format_str)
40+
.to_string()
41+
.parse()
42+
.map_err(|_| ZervError::InvalidFormat(format!("Failed to parse {component_type}")))
43+
}
44+
3445
pub fn resolve_timestamp(pattern: &str, timestamp: Option<u64>) -> Result<u64> {
3546
let ts = timestamp.ok_or_else(|| {
3647
ZervError::InvalidFormat("Timestamp is required but was None".to_string())
@@ -39,76 +50,20 @@ pub fn resolve_timestamp(pattern: &str, timestamp: Option<u64>) -> Result<u64> {
3950
.ok_or_else(|| ZervError::InvalidFormat("Invalid timestamp".to_string()))?;
4051

4152
let result = match pattern {
42-
"YYYY" => dt
43-
.format("%Y")
44-
.to_string()
45-
.parse()
46-
.map_err(|_| ZervError::InvalidFormat("Failed to parse year".to_string()))?,
47-
"YY" => dt
48-
.format("%y")
49-
.to_string()
50-
.parse()
51-
.map_err(|_| ZervError::InvalidFormat("Failed to parse year".to_string()))?,
52-
"MM" => dt
53-
.format("%-m")
54-
.to_string()
55-
.parse()
56-
.map_err(|_| ZervError::InvalidFormat("Failed to parse month".to_string()))?,
57-
"0M" => dt
58-
.format("%m")
59-
.to_string()
60-
.parse()
61-
.map_err(|_| ZervError::InvalidFormat("Failed to parse month".to_string()))?,
62-
"WW" => dt
63-
.format("%-W")
64-
.to_string()
65-
.parse()
66-
.map_err(|_| ZervError::InvalidFormat("Failed to parse week".to_string()))?,
67-
"0W" => dt
68-
.format("%W")
69-
.to_string()
70-
.parse()
71-
.map_err(|_| ZervError::InvalidFormat("Failed to parse week".to_string()))?,
72-
"DD" => dt
73-
.format("%-d")
74-
.to_string()
75-
.parse()
76-
.map_err(|_| ZervError::InvalidFormat("Failed to parse day".to_string()))?,
77-
"0D" => dt
78-
.format("%d")
79-
.to_string()
80-
.parse()
81-
.map_err(|_| ZervError::InvalidFormat("Failed to parse day".to_string()))?,
82-
"HH" => dt
83-
.format("%-H")
84-
.to_string()
85-
.parse()
86-
.map_err(|_| ZervError::InvalidFormat("Failed to parse hour".to_string()))?,
87-
"0H" => dt
88-
.format("%H")
89-
.to_string()
90-
.parse()
91-
.map_err(|_| ZervError::InvalidFormat("Failed to parse hour".to_string()))?,
92-
"mm" => dt
93-
.format("%-M")
94-
.to_string()
95-
.parse()
96-
.map_err(|_| ZervError::InvalidFormat("Failed to parse minute".to_string()))?,
97-
"0m" => dt
98-
.format("%M")
99-
.to_string()
100-
.parse()
101-
.map_err(|_| ZervError::InvalidFormat("Failed to parse minute".to_string()))?,
102-
"SS" => dt
103-
.format("%-S")
104-
.to_string()
105-
.parse()
106-
.map_err(|_| ZervError::InvalidFormat("Failed to parse second".to_string()))?,
107-
"0S" => dt
108-
.format("%S")
109-
.to_string()
110-
.parse()
111-
.map_err(|_| ZervError::InvalidFormat("Failed to parse second".to_string()))?,
53+
"YYYY" => parse_timestamp_component(&dt, "%Y", "year")?,
54+
"YY" => parse_timestamp_component(&dt, "%y", "year")?,
55+
"MM" => parse_timestamp_component(&dt, "%-m", "month")?,
56+
"0M" => parse_timestamp_component(&dt, "%m", "month")?,
57+
"WW" => parse_timestamp_component(&dt, "%-W", "week")?,
58+
"0W" => parse_timestamp_component(&dt, "%W", "week")?,
59+
"DD" => parse_timestamp_component(&dt, "%-d", "day")?,
60+
"0D" => parse_timestamp_component(&dt, "%d", "day")?,
61+
"HH" => parse_timestamp_component(&dt, "%-H", "hour")?,
62+
"0H" => parse_timestamp_component(&dt, "%H", "hour")?,
63+
"mm" => parse_timestamp_component(&dt, "%-M", "minute")?,
64+
"0m" => parse_timestamp_component(&dt, "%M", "minute")?,
65+
"SS" => parse_timestamp_component(&dt, "%-S", "second")?,
66+
"0S" => parse_timestamp_component(&dt, "%S", "second")?,
11267
_ => {
11368
return Err(ZervError::InvalidFormat(format!(
11469
"Unknown timestamp pattern: {pattern}"

0 commit comments

Comments
 (0)