mirror of
https://github.com/nushell/nushell
synced 2025-01-15 14:44:14 +00:00
Fix touch panics when using invalid timestamp (#6181)
Signed-off-by: nibon7 <nibon7@163.com>
This commit is contained in:
parent
2cffff0c1b
commit
9e24e452a5
2 changed files with 29 additions and 2 deletions
|
@ -140,14 +140,24 @@ impl Command for Touch {
|
||||||
10 => Some(AddYear::Full),
|
10 => Some(AddYear::Full),
|
||||||
12 => Some(AddYear::FirstDigits),
|
12 => Some(AddYear::FirstDigits),
|
||||||
14 => None,
|
14 => None,
|
||||||
_ => unreachable!(), // This should never happen as the check above should catch it
|
_ => {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"input has an invalid timestamp".to_string(),
|
||||||
|
span,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
match size {
|
match size {
|
||||||
8 => Some(AddYear::Full),
|
8 => Some(AddYear::Full),
|
||||||
10 => Some(AddYear::FirstDigits),
|
10 => Some(AddYear::FirstDigits),
|
||||||
12 => None,
|
12 => None,
|
||||||
_ => unreachable!(), // This should never happen as the check above should catch it
|
_ => {
|
||||||
|
return Err(ShellError::UnsupportedInput(
|
||||||
|
"input has an invalid timestamp".to_string(),
|
||||||
|
span,
|
||||||
|
))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -766,3 +766,20 @@ fn not_create_file_if_it_not_exists() {
|
||||||
assert!(!path.exists());
|
assert!(!path.exists());
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn test_invalid_timestamp() {
|
||||||
|
Playground::setup("test_invalid_timestamp", |dirs, _sandbox| {
|
||||||
|
let outcome = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
r#"touch -t 20220729. file.txt"#
|
||||||
|
);
|
||||||
|
assert!(outcome.err.contains("input has an invalid timestamp"));
|
||||||
|
|
||||||
|
let outcome = nu!(
|
||||||
|
cwd: dirs.test(),
|
||||||
|
r#"touch -t 20220729120099 file.txt"#
|
||||||
|
);
|
||||||
|
assert!(outcome.err.contains("input has an invalid timestamp"));
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue