mirror of
https://github.com/saltstack/salt.git
synced 2025-04-16 09:40:20 +00:00
Add tests
This commit is contained in:
parent
a4661b2002
commit
eed0c9e370
2 changed files with 94 additions and 2 deletions
|
@ -3541,9 +3541,9 @@ 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):
|
||||
|
|
92
tests/pytests/functional/modules/file/test_touch.py
Normal file
92
tests/pytests/functional/modules/file/test_touch.py
Normal file
|
@ -0,0 +1,92 @@
|
|||
"""
|
||||
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