Skip to content

Commit

Permalink
Return error early if seconds part of timestamp is invalid (#6193)
Browse files Browse the repository at this point in the history
Signed-off-by: nibon7 <nibon7@163.com>
  • Loading branch information
nibon7 committed Jul 31, 2022
1 parent dd2a0e3 commit 26caf7e
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 1 deletion.
10 changes: 9 additions & 1 deletion crates/nu-command/src/filesystem/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ impl Command for Touch {

// Checks for the seconds stamp and removes the '.' delimiter if any
let (val, has_sec): (String, bool) = match stamp.split_once('.') {
Some((dtime, sec)) => (format!("{}{}", dtime, sec), true),
Some((dtime, sec)) => match sec.parse::<u8>() {
Ok(sec) if sec < 60 => (format!("{}{}", dtime, sec), true),
_ => {
return Err(ShellError::UnsupportedInput(
"input has an invalid timestamp".to_string(),
span,
))
}
},
None => (stamp.to_string(), false),
};

Expand Down
84 changes: 84 additions & 0 deletions crates/nu-command/tests/commands/touch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,34 @@ fn errors_if_change_modified_time_of_file_with_invalid_timestamp() {
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -t 082412.3012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -t 0824.123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -t 08.24123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -t 0.824123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));
})
}

Expand Down Expand Up @@ -401,6 +429,34 @@ fn errors_if_change_access_time_of_file_with_invalid_timestamp() {
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -a -t 082412.3012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -a -t 0824.123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -a -t 08.24123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -a -t 0.824123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));
})
}

Expand Down Expand Up @@ -634,6 +690,34 @@ fn errors_if_change_modified_and_access_time_of_file_with_invalid_timestamp() {
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -a -t 082412.3012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -a -t 0824.123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -a -t 08.24123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));

outcome = nu!(
cwd: dirs.test(),
"touch -m -a -t 0.824123012 file.txt"
);

assert!(outcome.err.contains("input has an invalid timestamp"));
})
}

Expand Down

0 comments on commit 26caf7e

Please sign in to comment.