[Reproducible-commits] [diffoscope] 04/04: Give proper error message when run on non-existing files

Jérémy Bobbio lunar at moszumanska.debian.org
Thu Sep 3 09:40:36 UTC 2015


This is an automated email from the git hooks/post-receive script.

lunar pushed a commit to branch master
in repository diffoscope.

commit 9c7cc4825bf3c95475c7f66e1c6adf77f2601832
Author: Jérémy Bobbio <lunar at debian.org>
Date:   Thu Sep 3 07:57:23 2015 +0000

    Give proper error message when run on non-existing files
    
    Also first tests for main()!
---
 diffoscope/comparators/__init__.py |  5 +++++
 tests/test_main.py                 | 31 +++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)

diff --git a/diffoscope/comparators/__init__.py b/diffoscope/comparators/__init__.py
index 3181985..6027462 100644
--- a/diffoscope/comparators/__init__.py
+++ b/diffoscope/comparators/__init__.py
@@ -62,6 +62,11 @@ from diffoscope.comparators.zip import ZipFile
 
 
 def compare_root_paths(path1, path2):
+    if not all(map(os.path.lexists, (path1, path2))):
+        for path in (path1, path2):
+            if not os.path.lexists(path):
+                sys.stderr.write('%s: %s: No such file or directory\n' % (sys.argv[0], path))
+        sys.exit(2)
     if os.path.isdir(path1) and os.path.isdir(path2):
         return compare_directories(path1, path2)
     return compare_files(specialize(FilesystemFile(path1)), specialize(FilesystemFile(path2)))
diff --git a/tests/test_main.py b/tests/test_main.py
new file mode 100644
index 0000000..acc963f
--- /dev/null
+++ b/tests/test_main.py
@@ -0,0 +1,31 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright © 2015 Jérémy Bobbio <lunar at 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
+# along with diffoscope.  If not, see <http://www.gnu.org/licenses/>.
+
+import pytest
+from diffoscope.__main__ import main
+
+def test_non_existing_files(capsys):
+    args = '/nonexisting1 /nonexisting2'
+    with pytest.raises(SystemExit) as excinfo:
+        main(args.split())
+    assert excinfo.value.code == 2
+    out, err = capsys.readouterr()
+    assert '/nonexisting1: No such file or directory' in err
+    assert '/nonexisting2: No such file or directory' in err

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/reproducible/diffoscope.git



More information about the Reproducible-commits mailing list