Add tests

This commit is contained in:
Twangboy 2022-11-14 13:05:49 -07:00
parent a4661b2002
commit eed0c9e370
No known key found for this signature in database
GPG key ID: ED267D5C0DE6F8A6
2 changed files with 94 additions and 2 deletions

View file

@ -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):

View 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