Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • anlambert/swh-model
  • lunar/swh-model
  • franckbret/swh-model
  • douardda/swh-model
  • olasd/swh-model
  • swh/devel/swh-model
  • Alphare/swh-model
  • samplet/swh-model
  • marmoute/swh-model
  • rboyer/swh-model
10 results
Show changes
Showing
with 4926 additions and 902 deletions
...@@ -157,7 +157,9 @@ class ValidateCompound(unittest.TestCase): ...@@ -157,7 +157,9 @@ class ValidateCompound(unittest.TestCase):
def test_validate_whole_schema_shortcut_previous_error(self): def test_validate_whole_schema_shortcut_previous_error(self):
with self.assertRaises(ValidationError) as cm: with self.assertRaises(ValidationError) as cm:
compound.validate_against_schema( compound.validate_against_schema(
self.test_model, self.test_schema_shortcut, self.test_value_missing, self.test_model,
self.test_schema_shortcut,
self.test_value_missing,
) )
exc = cm.exception exc = cm.exception
...@@ -167,7 +169,9 @@ class ValidateCompound(unittest.TestCase): ...@@ -167,7 +169,9 @@ class ValidateCompound(unittest.TestCase):
def test_validate_whole_schema(self): def test_validate_whole_schema(self):
with self.assertRaises(ValidationError) as cm: with self.assertRaises(ValidationError) as cm:
compound.validate_against_schema( compound.validate_against_schema(
self.test_model, self.test_schema_shortcut, self.test_value, self.test_model,
self.test_schema_shortcut,
self.test_value,
) )
# The exception should be of the form: # The exception should be of the form:
......
...@@ -4,12 +4,12 @@ ...@@ -4,12 +4,12 @@
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
from datetime import datetime from datetime import datetime
from pytz import all_timezones, timezone
from random import choice, randint, random, shuffle from random import choice, randint, random, shuffle
from typing import List, Dict from typing import Dict, List
from swh.model.hashutil import MultiHash from pytz import all_timezones, timezone
from swh.model.hashutil import MultiHash
PROTOCOLS = ["git", "http", "https", "deb", "svn", "mock"] PROTOCOLS = ["git", "http", "https", "deb", "svn", "mock"]
DOMAINS = ["example.com", "some.long.host.name", "xn--n28h.tld"] DOMAINS = ["example.com", "some.long.host.name", "xn--n28h.tld"]
......
...@@ -7,7 +7,7 @@ from operator import itemgetter ...@@ -7,7 +7,7 @@ from operator import itemgetter
import os import os
import sys import sys
from swh.model.from_disk import Directory, DentryPerms from swh.model.from_disk import DentryPerms, Directory
from swh.model.hashutil import ALGORITHMS, hash_to_hex from swh.model.hashutil import ALGORITHMS, hash_to_hex
......
This diff is collapsed.
...@@ -4,26 +4,28 @@ ...@@ -4,26 +4,28 @@
# See top-level LICENSE file for more information # See top-level LICENSE file for more information
import os import os
import sys
import tarfile import tarfile
import tempfile import tempfile
import unittest import unittest
import unittest.mock
from click.testing import CliRunner from click.testing import CliRunner
import pytest import pytest
from swh.model import cli from swh.model import cli
from swh.model.hashutil import hash_to_hex from swh.model.hashutil import hash_to_hex
from swh.model.tests.swh_model_data import SAMPLE_FOLDER_SWHIDS
from swh.model.tests.test_from_disk import DataMixin from swh.model.tests.test_from_disk import DataMixin
@pytest.mark.fs
class TestIdentify(DataMixin, unittest.TestCase): class TestIdentify(DataMixin, unittest.TestCase):
def setUp(self): def setUp(self):
super().setUp() super().setUp()
self.runner = CliRunner() self.runner = CliRunner()
def assertSWHID(self, result, swhid): def assertSWHID(self, result, swhid):
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0, result.output)
self.assertEqual(result.output.split()[0], swhid) self.assertEqual(result.output.split()[0], swhid)
def test_no_args(self): def test_no_args(self):
...@@ -52,6 +54,7 @@ class TestIdentify(DataMixin, unittest.TestCase): ...@@ -52,6 +54,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
result = self.runner.invoke(cli.identify, ["--type", "directory", path]) result = self.runner.invoke(cli.identify, ["--type", "directory", path])
self.assertSWHID(result, "swh:1:dir:e8b0f1466af8608c8a3fb9879db172b887e80759") self.assertSWHID(result, "swh:1:dir:e8b0f1466af8608c8a3fb9879db172b887e80759")
@pytest.mark.requires_optional_deps
def test_snapshot_id(self): def test_snapshot_id(self):
"""identify a snapshot""" """identify a snapshot"""
tarball = os.path.join( tarball = os.path.join(
...@@ -68,6 +71,20 @@ class TestIdentify(DataMixin, unittest.TestCase): ...@@ -68,6 +71,20 @@ class TestIdentify(DataMixin, unittest.TestCase):
result, "swh:1:snp:abc888898124270905a0ef3c67e872ce08e7e0c1" result, "swh:1:snp:abc888898124270905a0ef3c67e872ce08e7e0c1"
) )
def test_snapshot_without_dulwich(self):
"""checks swh-identify returns a 'nice' message instead of a traceback
when dulwich is not installed"""
with unittest.mock.patch.dict(sys.modules, {"dulwich": None}):
with tempfile.TemporaryDirectory(prefix="swh.model.cli") as d:
result = self.runner.invoke(
cli.identify,
["--type", "snapshot", d],
catch_exceptions=False,
)
assert result.exit_code == 1
assert "'swh.model[cli]'" in result.output
def test_origin_id(self): def test_origin_id(self):
"""identify an origin URL""" """identify an origin URL"""
url = "https://github.com/torvalds/linux" url = "https://github.com/torvalds/linux"
...@@ -78,7 +95,8 @@ class TestIdentify(DataMixin, unittest.TestCase): ...@@ -78,7 +95,8 @@ class TestIdentify(DataMixin, unittest.TestCase):
"""identify symlink --- both itself and target""" """identify symlink --- both itself and target"""
regular = os.path.join(self.tmpdir_name, b"foo.txt") regular = os.path.join(self.tmpdir_name, b"foo.txt")
link = os.path.join(self.tmpdir_name, b"bar.txt") link = os.path.join(self.tmpdir_name, b"bar.txt")
open(regular, "w").write("foo\n") with open(regular, "w") as f:
f.write("foo\n")
os.symlink(os.path.basename(regular), link) os.symlink(os.path.basename(regular), link)
result = self.runner.invoke(cli.identify, [link]) result = self.runner.invoke(cli.identify, [link])
...@@ -127,7 +145,7 @@ class TestIdentify(DataMixin, unittest.TestCase): ...@@ -127,7 +145,7 @@ class TestIdentify(DataMixin, unittest.TestCase):
def test_auto_origin(self): def test_auto_origin(self):
"""automatic object type detection: origin""" """automatic object type detection: origin"""
result = self.runner.invoke(cli.identify, ["https://github.com/torvalds/linux"]) result = self.runner.invoke(cli.identify, ["https://github.com/torvalds/linux"])
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0, result.output)
self.assertRegex(result.output, r"^swh:\d+:ori:") self.assertRegex(result.output, r"^swh:\d+:ori:")
def test_verify_content(self): def test_verify_content(self):
...@@ -139,10 +157,57 @@ class TestIdentify(DataMixin, unittest.TestCase): ...@@ -139,10 +157,57 @@ class TestIdentify(DataMixin, unittest.TestCase):
# match # match
path = os.path.join(self.tmpdir_name, filename) path = os.path.join(self.tmpdir_name, filename)
result = self.runner.invoke(cli.identify, ["--verify", expected_id, path]) result = self.runner.invoke(cli.identify, ["--verify", expected_id, path])
self.assertEqual(result.exit_code, 0) self.assertEqual(result.exit_code, 0, result.output)
# mismatch # mismatch
with open(path, "a") as f: with open(path, "a") as f:
f.write("trailing garbage to make verification fail") f.write("trailing garbage to make verification fail")
result = self.runner.invoke(cli.identify, ["--verify", expected_id, path]) result = self.runner.invoke(cli.identify, ["--verify", expected_id, path])
self.assertEqual(result.exit_code, 1) self.assertEqual(result.exit_code, 1)
def test_exclude(self):
"""exclude patterns"""
self.make_from_tarball(self.tmpdir_name)
path = os.path.join(self.tmpdir_name, b"sample-folder")
excluded_dir = os.path.join(path, b"excluded_dir\x96")
os.mkdir(excluded_dir)
with open(os.path.join(excluded_dir, b"some_file"), "w") as f:
f.write("content")
result = self.runner.invoke(
cli.identify, ["--type", "directory", "--exclude", "excluded_*", path]
)
self.assertSWHID(result, "swh:1:dir:e8b0f1466af8608c8a3fb9879db172b887e80759")
def test_recursive_directory(self):
self.make_from_tarball(self.tmpdir_name)
path = os.path.join(self.tmpdir_name, b"sample-folder")
result = self.runner.invoke(cli.identify, ["--recursive", path])
self.assertEqual(result.exit_code, 0, result.output)
result = result.output.split()
result_swhids = []
# get all SWHID from the result
for i in range(0, len(result)):
if i % 2 == 0:
result_swhids.append(result[i])
assert len(result_swhids) == len(SAMPLE_FOLDER_SWHIDS)
for swhid in SAMPLE_FOLDER_SWHIDS:
assert swhid in result_swhids
def test_recursive_directory_no_filename(self):
self.make_from_tarball(self.tmpdir_name)
path = os.path.join(self.tmpdir_name, b"sample-folder")
result = self.runner.invoke(
cli.identify, ["--recursive", "--no-filename", path]
)
self.assertEqual(result.exit_code, 0, result.output)
result_swhids = result.output.split()
assert len(result_swhids) == len(SAMPLE_FOLDER_SWHIDS)
for swhid in SAMPLE_FOLDER_SWHIDS:
assert swhid in result_swhids
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.