mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Merge pull request #57222 from alexey-zhukovin/master-port-49310
Port #49310 to master
This commit is contained in:
commit
7d0c77409b
3 changed files with 105 additions and 7 deletions
1
changelog/49310.fixed
Normal file
1
changelog/49310.fixed
Normal file
|
@ -0,0 +1 @@
|
|||
Issue #49310: Allow users to touch a file with Unix date of birth
|
|
@ -3525,9 +3525,13 @@ def touch(name, atime=None, mtime=None):
|
|||
simply update the atime and mtime if it already does.
|
||||
|
||||
atime:
|
||||
Access time in Unix epoch time
|
||||
Access time in Unix epoch time. Set it to 0 to set atime of the
|
||||
file with Unix date of birth. If this parameter isn't set, atime
|
||||
will be set with current time.
|
||||
mtime:
|
||||
Last modification in Unix epoch time
|
||||
Last modification in Unix epoch time. Set it to 0 to set mtime of
|
||||
the file with Unix date of birth. If this parameter isn't set,
|
||||
mtime will be set with current time.
|
||||
|
||||
CLI Example:
|
||||
|
||||
|
@ -3537,20 +3541,20 @@ def touch(name, atime=None, mtime=None):
|
|||
"""
|
||||
name = os.path.expanduser(name)
|
||||
|
||||
if atime and atime.isdigit():
|
||||
if atime and str(atime).isdigit():
|
||||
atime = int(atime)
|
||||
if mtime and mtime.isdigit():
|
||||
if mtime and str(mtime).isdigit():
|
||||
mtime = int(mtime)
|
||||
try:
|
||||
if not os.path.exists(name):
|
||||
with salt.utils.files.fopen(name, "a"):
|
||||
pass
|
||||
|
||||
if not atime and not mtime:
|
||||
if atime is None and mtime is None:
|
||||
times = None
|
||||
elif not mtime and atime:
|
||||
elif mtime is None and atime is not None:
|
||||
times = (atime, time.time())
|
||||
elif not atime and mtime:
|
||||
elif atime is None and mtime is not None:
|
||||
times = (time.time(), mtime)
|
||||
else:
|
||||
times = (atime, mtime)
|
||||
|
|
93
tests/pytests/functional/modules/file/test_touch.py
Normal file
93
tests/pytests/functional/modules/file/test_touch.py
Normal file
|
@ -0,0 +1,93 @@
|
|||
"""
|
||||
Tests for file.touch function
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
import pytest
|
||||
|
||||
from salt.exceptions import SaltInvocationError
|
||||
|
||||
pytestmark = [
|
||||
pytest.mark.windows_whitelisted,
|
||||
]
|
||||
|
||||
|
||||
@pytest.fixture(scope="module")
|
||||
def file(modules):
|
||||
return modules.file
|
||||
|
||||
|
||||
def test_touch(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target))
|
||||
assert os.path.exists(target)
|
||||
|
||||
|
||||
def test_touch_error_atime(file, tmp_path):
|
||||
"""
|
||||
Test touch with non int input
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
with pytest.raises(SaltInvocationError) as exc:
|
||||
file.touch(str(target), atime="string")
|
||||
assert "atime and mtime must be integers" in exc.value.message
|
||||
|
||||
|
||||
def test_touch_error_mtime(file, tmp_path):
|
||||
"""
|
||||
Test touch with non int input
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
with pytest.raises(SaltInvocationError) as exc:
|
||||
file.touch(str(target), mtime="string")
|
||||
assert "atime and mtime must be integers" in exc.value.message
|
||||
|
||||
|
||||
def test_touch_atime(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target), atime=123)
|
||||
assert os.stat(str(target)).st_atime == 123
|
||||
|
||||
|
||||
def test_touch_atime_zero(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target), atime=0)
|
||||
assert os.stat(str(target)).st_atime == 0
|
||||
|
||||
|
||||
def test_touch_mtime(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target), mtime=234)
|
||||
assert os.stat(str(target)).st_mtime == 234
|
||||
|
||||
|
||||
def test_touch_mtime_zero(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target), mtime=0)
|
||||
assert os.stat(str(target)).st_mtime == 0
|
||||
|
||||
|
||||
def test_touch_atime_mtime(file, tmp_path):
|
||||
"""
|
||||
Test touch with defaults
|
||||
"""
|
||||
target = tmp_path / "test.file"
|
||||
file.touch(str(target), atime=456, mtime=789)
|
||||
assert os.stat(str(target)).st_atime == 456
|
||||
assert os.stat(str(target)).st_mtime == 789
|
Loading…
Add table
Reference in a new issue