Skip to content

Commit

Permalink
Improve test coverage of utils.py (ansible#615)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shrews authored Oct 23, 2023
1 parent 9f1303d commit 66ca6b7
Showing 1 changed file with 40 additions and 3 deletions.
43 changes: 40 additions & 3 deletions test/unit/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import filecmp
import os
import pathlib

import pytest

from ansible_builder.utils import write_file, copy_file, run_command
from ansible_builder.utils import configure_logger, write_file, copy_directory, copy_file, run_command


def test_write_file(tmp_path):
path = tmp_path / 'foo.txt'
path = tmp_path / 'bar' / 'foo.txt'
text = [
'foo # from some collection',
'bar',
Expand All @@ -26,6 +27,7 @@ def test_copy_file(dest_file, source_file):

assert copy_file(source_file, dest_file)
assert not copy_file(source_file, dest_file)
assert not copy_file(source_file, source_file)


def test_copy_touched_file(dest_file, source_file):
Expand Down Expand Up @@ -65,8 +67,9 @@ def test_copy_file_with_destination_directory(dest_file, source_file):
@pytest.mark.run_command
def test_failed_command(mocker):
mocker.patch('ansible_builder.utils.subprocess.Popen.wait', return_value=1)
configure_logger(3)
with pytest.raises(SystemExit):
run_command(['sleep', '--invalidargument'])
run_command(['sleep', '--invalidargument'], capture_output=True)


@pytest.mark.run_command
Expand Down Expand Up @@ -108,3 +111,37 @@ def test_invalid_docker_command(caplog, mocker):

assert 'You do not have docker installed' in record.msg
assert 'podman: not installed, docker: not installed' in record.msg


def test_copy_directory_notadir(tmp_path):
"""
Test passing a file instead of a directory.
"""
notadir = tmp_path / 'xyz'
notadir.touch()
with pytest.raises(Exception, match="Expected a directory at *"):
copy_directory(notadir, 'abc')


def test_copy_directory(tmp_path):
src = tmp_path / "src"
src.mkdir()

# a file
src_f1 = src / "f1"
src_f1.touch()

# a subdirectory and a file underneath it
src_d1 = src / "d1"
src_d1.mkdir()
src_d1f2 = src_d1 / "f2"
src_d1f2.touch()

dst = tmp_path / "dst"
dst.mkdir()

copy_directory(src, dst)

dcmp = filecmp.dircmp(str(src), str(dst))
assert not dcmp.left_only
assert not dcmp.right_only

0 comments on commit 66ca6b7

Please sign in to comment.