test_main.py 4.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
# -*- coding: utf-8 -*-
#
# diffoscope: in-depth comparison of files, archives, and directories
#
# Copyright © 2015 Jérémy Bobbio <lunar@debian.org>
#
# diffoscope is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# diffoscope is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
18
# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
19

20
import os
Chris Lamb's avatar
Chris Lamb committed
21
import pytest
22 23
import signal
import tempfile
Chris Lamb's avatar
Chris Lamb committed
24

25
from diffoscope.main import main
26

27 28 29 30 31 32 33 34
TEST_TAR1_PATH = os.path.join(os.path.dirname(__file__), 'data/test1.tar')
TEST_TAR2_PATH = os.path.join(os.path.dirname(__file__), 'data/test2.tar')
TEST_TARS = (TEST_TAR1_PATH, TEST_TAR2_PATH)


def run(capsys, *args):
    with pytest.raises(SystemExit) as exc:
        main(args)
Chris Lamb's avatar
Chris Lamb committed
35

36
    out, err = capsys.readouterr()
37 38 39

    return exc.value.code, out, err

40

41 42 43 44
def test_non_existing_files(capsys):
    ret, _, err = run(capsys, '/nonexisting1', '/nonexisting2')

    assert ret == 2
45 46
    assert '/nonexisting1: No such file or directory' in err
    assert '/nonexisting2: No such file or directory' in err
47

48

49
def test_non_existing_left_with_new_file(capsys):
50 51 52
    ret, out, _ = run(capsys, '--new-file', '/nonexisting1', __file__)

    assert ret == 1
53 54 55
    assert '--- /nonexisting1' in out
    assert ('+++ %s' % __file__) in out

56

57
def test_non_existing_right_with_new_file(capsys):
58 59 60
    ret, out, _ = run(capsys, '--new-file', __file__, '/nonexisting2')

    assert ret == 1
61 62 63
    assert ('--- %s' % __file__) in out
    assert '+++ /nonexisting2' in out

64

65
def test_non_existing_files_with_new_file(capsys):
66 67 68
    ret, out, _ = run(capsys, '--new-file', '/nonexisting1', '/nonexisting2')

    assert ret == 1
69 70 71
    assert '--- /nonexisting1' in out
    assert '+++ /nonexisting2' in out
    assert 'Trying to compare two non-existing files.' in out
72

73

74
def test_remove_temp_files_on_sigterm(capsys, tmpdir, monkeypatch):
75
    pid = os.fork()
76

77 78 79 80 81
    if pid == 0:
        def suicide(*args):
            os.kill(os.getpid(), signal.SIGTERM)
        monkeypatch.setattr('diffoscope.comparators.text.TextFile.compare', suicide)
        tempfile.tempdir = str(tmpdir)
82 83 84

        ret, _, _ = run(capsys, *TEST_TARS)
        os._exit(ret)
85 86
    else:
        _, ret = os.waitpid(pid, 0)
Mattia Rizzolo's avatar
Mattia Rizzolo committed
87
        assert (ret >> 8) == 2  # having received SIGTERM is trouble
88
        assert os.listdir(str(tmpdir)) == []
89

90

91 92
def test_ctrl_c_handling(tmpdir, monkeypatch, capsys):
    monkeypatch.setattr('tempfile.tempdir', str(tmpdir))
93

94 95
    def interrupt(*args):
        raise KeyboardInterrupt
96 97 98 99 100 101 102
    monkeypatch.setattr(
        'diffoscope.comparators.text.TextFile.compare',
        interrupt,
    )

    ret, _, err = run(capsys, *TEST_TARS)

103
    assert '' in err
104
    assert ret == 2
105
    assert os.listdir(str(tmpdir)) == []
106

107

108
def test_no_differences(capsys):
109 110 111
    ret, out, err = run(capsys, TEST_TAR1_PATH, TEST_TAR1_PATH)

    assert ret == 0
112 113
    assert err == ''
    assert out == ''
114

115

116
def test_no_differences_directories(capsys, tmpdir):
117 118
    def create_dir(x):
        path = str(tmpdir.mkdir(x))
Mattia Rizzolo's avatar
Mattia Rizzolo committed
119
        os.utime(path, (0, 0))  # Ensure consistent mtime
120 121 122
        return path

    ret, out, err = run(capsys, create_dir('a'), create_dir('b'))
123 124

    assert ret == 0
125 126 127
    # if getfacl is not available there will be a warning message on stderr
    # if it's available then err should be empty
    assert err == '' or "getfacl" in err
128 129
    assert out == ''

130

131
def test_list_tools(capsys):
132 133 134
    ret, out, err = run(capsys, '--list-tools')

    assert ret == 0
135
    assert err == ''
136
    assert 'External-Tools-Required: ' in out
137
    assert 'xxd,' in out
138

139

140 141 142 143 144 145
def test_profiling(capsys):
    ret, out, err = run(capsys, TEST_TAR1_PATH, TEST_TAR1_PATH, '--profile=-')

    assert ret == 0
    assert "Profiling output for" in out
    assert err == ''