Merge ~jslarraz/review-tools:add-container-exceptions-and-tests-framework into review-tools:master

Proposed by Jorge Sancho Larraz
Status: Merged
Merged at revision: d02c70de67d1ee0b90672eea8bd2fda11e2cc72e
Proposed branch: ~jslarraz/review-tools:add-container-exceptions-and-tests-framework
Merge into: review-tools:master
Diff against target: 180 lines (+76/-12)
5 files modified
reviewtools/containers/base_container.py (+26/-9)
reviewtools/sr_common.py (+5/-2)
reviewtools/sr_lint.py (+6/-1)
reviewtools/tests/containers/__init__.py (+0/-0)
reviewtools/tests/containers/test_base_container.py (+39/-0)
Reviewer Review Type Date Requested Status
Nishit Majithia Approve
Review via email: mp+466372@code.launchpad.net

Commit message

Add container exception and tests framework

To post a comment you must log in.
Revision history for this message
Nishit Majithia (0xnishit) wrote :

thanks for adding tests and handling error more standardize
lgtm!!

review: Approve
Revision history for this message
Alex Murray (alexmurray) wrote :

Thanks for this - one comment though, normally exceptions have a class or type - so I would expect this to be called 'InvalidContainerExtension' say rather than just 'ContainerException' - or perhaps 'BaseContainerException' - just wondering whether you think the name should be changed?

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/reviewtools/containers/base_container.py b/reviewtools/containers/base_container.py
index a3f5f4a..a9e0f41 100644
--- a/reviewtools/containers/base_container.py
+++ b/reviewtools/containers/base_container.py
@@ -3,7 +3,25 @@ import shutil
3import magic3import magic
4from tempfile import mkdtemp4from tempfile import mkdtemp
55
6from reviewtools.common import MKDTEMP_PREFIX, MKDTEMP_DIR, error, debug, unpack_pkg, recursive_rm, open_file_read6from reviewtools.common import (
7 MKDTEMP_PREFIX,
8 MKDTEMP_DIR,
9 debug,
10 unpack_pkg,
11 recursive_rm,
12 open_file_read,
13)
14
15
16class ContainerException(Exception):
17 """This class represents exceptions"""
18
19 def __init__(self, value):
20 self.value = value
21
22 def __str__(self):
23 return str(self.value)
24
725
8class BaseContainer:26class BaseContainer:
9 """27 """
@@ -31,14 +49,14 @@ class BaseContainer:
31 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)49 self.tmp_dir = mkdtemp(prefix=MKDTEMP_PREFIX, dir=MKDTEMP_DIR)
32 if os.path.isfile(fn):50 if os.path.isfile(fn):
33 if os.path.basename(fn).count(".") == 0:51 if os.path.basename(fn).count(".") == 0:
34 error("container filename must include an extension")52 raise ContainerException("container filename must include an extension")
35 self.filename = os.path.join(53 self.filename = os.path.join(
36 self.tmp_dir,54 self.tmp_dir,
37 os.path.basename(fn),55 os.path.basename(fn),
38 )56 )
39 shutil.copyfile(fn, self.filename)57 shutil.copyfile(fn, self.filename)
40 else:58 else:
41 error("Could not find '%s'" % fn)59 raise ContainerException("Could not find '%s'" % fn)
4260
43 self.unpack_dir = ".".join(self.filename.split(".")[:-1])61 self.unpack_dir = ".".join(self.filename.split(".")[:-1])
44 unpack_pkg(fn, dest=self.unpack_dir)62 unpack_pkg(fn, dest=self.unpack_dir)
@@ -62,11 +80,11 @@ class BaseContainer:
62 def _extract_file(self, fn):80 def _extract_file(self, fn):
63 """Extract file"""81 """Extract file"""
64 if not fn.startswith("/"):82 if not fn.startswith("/"):
65 error("_extract_file() expects absolute path")83 raise ContainerException("_extract_file() expects absolute path")
66 rel = os.path.relpath(fn, self.unpack_dir)84 rel = os.path.relpath(fn, self.unpack_dir)
6785
68 if not os.path.isfile(fn):86 if not os.path.isfile(fn):
69 error("Could not find '%s'" % rel)87 raise ContainerException("Could not find '%s'" % rel)
70 return open_file_read(fn)88 return open_file_read(fn)
7189
72 def get_files_list(self):90 def get_files_list(self):
@@ -91,11 +109,10 @@ class BaseContainer:
91 continue109 continue
92110
93 if (111 if (
94 res in self.magic_binary_file_descriptions112 res in self.magic_binary_file_descriptions
95 and not i.endswith(".mo") # check is not a message catalog113 and not i.endswith(".mo") # check is not a message catalog
96 and i not in bin_files114 and i not in bin_files
97 ):115 ):
98 bin_files.append(i)116 bin_files.append(i)
99117
100 return bin_files118 return bin_files
101
diff --git a/reviewtools/sr_common.py b/reviewtools/sr_common.py
index ca8960f..9bdb556 100644
--- a/reviewtools/sr_common.py
+++ b/reviewtools/sr_common.py
@@ -30,7 +30,7 @@ from reviewtools.common import (
30 unsquashfs_lln_parse,30 unsquashfs_lln_parse,
31 verify_type,31 verify_type,
32)32)
33from reviewtools.containers.base_container import BaseContainer33from reviewtools.containers.base_container import BaseContainer, ContainerException
34from reviewtools.overrides import interfaces_attribs_addons34from reviewtools.overrides import interfaces_attribs_addons
3535
3636
@@ -417,7 +417,10 @@ class SnapReview(ReviewBase):
417 return417 return
418 ReviewBase.__init__(self, review_type, overrides=overrides)418 ReviewBase.__init__(self, review_type, overrides=overrides)
419419
420 self.snap = BaseContainer(fn)420 try:
421 self.snap = BaseContainer(fn)
422 except ContainerException as e:
423 error(str(e))
421 # TODO: update code to directly use self.snap.xxx instead of assigning here424 # TODO: update code to directly use self.snap.xxx instead of assigning here
422 self.pkg_filename = self.snap.filename425 self.pkg_filename = self.snap.filename
423 self.unpack_dir = self.snap.unpack_dir426 self.unpack_dir = self.snap.unpack_dir
diff --git a/reviewtools/sr_lint.py b/reviewtools/sr_lint.py
index 40990ad..518e9a2 100644
--- a/reviewtools/sr_lint.py
+++ b/reviewtools/sr_lint.py
@@ -16,9 +16,11 @@
1616
17from __future__ import print_function17from __future__ import print_function
18from reviewtools.schemas.schema_validator import validate18from reviewtools.schemas.schema_validator import validate
19from reviewtools.containers.base_container import ContainerException
19from reviewtools.sr_common import SnapReview20from reviewtools.sr_common import SnapReview
20from reviewtools.common import (21from reviewtools.common import (
21 find_external_symlinks,22 find_external_symlinks,
23 error,
22)24)
23from reviewtools.overrides import (25from reviewtools.overrides import (
24 redflagged_snap_types_overrides,26 redflagged_snap_types_overrides,
@@ -1979,7 +1981,10 @@ class SnapReviewLint(SnapReview):
1979 else:1981 else:
1980 appnames.append("%s.%s" % (self.snap_yaml["name"], app))1982 appnames.append("%s.%s" % (self.snap_yaml["name"], app))
19811983
1982 fh = self.snap._extract_file(fn)1984 try:
1985 fh = self.snap._extract_file(fn)
1986 except ContainerException as e:
1987 error(str(e))
1983 lines = fh.readlines()1988 lines = fh.readlines()
1984 fh.close()1989 fh.close()
19851990
diff --git a/reviewtools/tests/containers/__init__.py b/reviewtools/tests/containers/__init__.py
1986new file mode 1006441991new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/reviewtools/tests/containers/__init__.py
diff --git a/reviewtools/tests/containers/test_base_container.py b/reviewtools/tests/containers/test_base_container.py
1987new file mode 1006441992new file mode 100644
index 0000000..4d0563f
--- /dev/null
+++ b/reviewtools/tests/containers/test_base_container.py
@@ -0,0 +1,39 @@
1import os
2import shutil
3import unittest
4from tempfile import mkdtemp
5from reviewtools.containers.base_container import BaseContainer, ContainerException
6from reviewtools.tests.utils import make_snap2
7
8
9class TestBaseContainer(unittest.TestCase):
10 """Tests for reviewtools.containers.base_container.BaseContainer"""
11
12 @classmethod
13 def setUpClass(cls):
14 cls.tmp_dir = mkdtemp()
15 cls.fn = make_snap2(
16 output_dir=cls.tmp_dir,
17 )
18
19 @classmethod
20 def tearDownClass(cls):
21 if os.path.exists(cls.tmp_dir):
22 shutil.rmtree(cls.tmp_dir)
23
24 # Test class initialization
25 def test_initialization_container__happy(self):
26 try:
27 BaseContainer(self.fn)
28 except Exception:
29 self.fail(
30 "An unexpected error occurred during BaseContainer initialization with container file"
31 )
32
33 def test_initialization__missing_file(self):
34 with self.assertRaises(ContainerException) as context:
35 BaseContainer("/non/existing/path")
36 self.assertTrue(
37 "does not exists or has incorrect permissions"
38 in context.exception.value
39 )

Subscribers

People subscribed via source and target branches