Skip to content

Commit

Permalink
Fixes double class definition in test_runlib
Browse files Browse the repository at this point in the history
  • Loading branch information
lukpueh committed Jan 6, 2017
1 parent 24956a9 commit 9ba7693
Showing 1 changed file with 39 additions and 81 deletions.
120 changes: 39 additions & 81 deletions test/test_runlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@
from in_toto.util import (generate_and_write_rsa_keypair,
prompt_import_rsa_key_from_file)




class Test_ApplyExcludePatterns(unittest.TestCase):
"""Test _apply_exclude_patterns(names, exclude_patterns) """

Expand Down Expand Up @@ -91,19 +88,25 @@ def setUpClass(self):
|-- bar
|-- foo
`-- subdir
|-- foosub
|-- foosub1
|-- foosub2
`-- subsubdir
`-- foosubsub
"""

self.working_dir = os.getcwd()

# Clear user set excludes
# Backup and clear user set excludes and base path
self.artifact_exclude_orig = settings.ARTIFACT_EXCLUDES
self.artifact_base_path_orig = settings.ARTIFACT_BASE_PATH
settings.ARTIFACT_EXCLUDES = []
settings.ARTIFACT_BASE_PATH = None

self.test_dir = tempfile.mkdtemp()
# mkdtemp uses $TMPDIR, which might contain a symlink
# but we want the absolute location instead
self.test_dir = os.path.realpath(tempfile.mkdtemp())
os.chdir(self.test_dir)

open("foo", "w").write("foo")
open("bar", "w").write("bar")

Expand All @@ -115,13 +118,41 @@ def setUpClass(self):

@classmethod
def tearDownClass(self):
"""Change back to initial working dir and remove temp test directory. """
"""Change back to working dir, remove temp directory, restore settings. """
os.chdir(self.working_dir)
shutil.rmtree(self.test_dir)
settings.ARTIFACT_EXCLUDES = self.artifact_exclude_orig
settings.ARTIFACT_BASE_PATH = self.artifact_base_path_orig

def tearDown(self):
"""Clear the ARTIFACT_EXLCUDES after every test. """
settings.ARTIFACT_EXCLUDES = []
settings.ARTIFACT_BASE_PATH = None

def test_not_existing_base_path(self):
"""Raise exception with not existing base path setting. """
settings.ARTIFACT_BASE_PATH = "path_does_not_exist"
with self.assertRaises(OSError):
record_artifacts_as_dict(["."])

def test_base_path_is_child_dir(self):
"""Test path of recorded artifacts and cd back with child as base."""
settings.ARTIFACT_BASE_PATH = "subdir"
artifacts_dict = record_artifacts_as_dict(["."])
self.assertListEqual(sorted(artifacts_dict.keys()),
sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
self.assertEquals(os.getcwd(), self.test_dir)

def test_base_path_is_parent_dir(self):
"""Test path of recorded artifacts and cd back with parent as base. """
settings.ARTIFACT_BASE_PATH = ".."
os.chdir("subdir/subsubdir")
artifacts_dict = record_artifacts_as_dict(["."])
self.assertListEqual(sorted(artifacts_dict.keys()),
sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
self.assertEquals(os.getcwd(),
os.path.join(self.test_dir, "subdir/subsubdir"))
os.chdir(self.test_dir)

def test_empty_artifacts_list_record_nothing(self):
"""Empty list passed. Return empty dict. """
Expand Down Expand Up @@ -174,7 +205,6 @@ def test_exclude_files_in_subdir(self):
self.assertListEqual(sorted(artifacts_dict.keys()),
sorted(["bar", "foo", "subdir/subsubdir/foosubsub"]))


def test_exclude_subsubdir(self):
"""Traverse dot. Exclude subsubdir. """
settings.ARTIFACT_EXCLUDES = ["*subsubdir"]
Expand All @@ -187,78 +217,6 @@ def test_exclude_subsubdir(self):
sorted(["foo", "bar", "subdir/foosub1", "subdir/foosub2"]))


class TestRecordArtifactsAsDict(unittest.TestCase):
"""Test record_artifacts_as_dict(artifacts). """

@classmethod
def setUpClass(self):
"""Create and change into temp test directory with dummy artifacts.
|-- bar
|-- foo
`-- subdir
|-- foosub1
|-- foosub2
`-- subsubdir
`-- foosubsub
"""

self.working_dir = os.getcwd()

# Clear user set excludes
settings.ARTIFACT_EXCLUDES = []
settings.ARTIFACT_BASE_PATH = None

# mkdtemp uses $TMPDIR, which might contain a symlink
# but we want the absolute location instead
self.test_dir = os.path.realpath(tempfile.mkdtemp())
os.chdir(self.test_dir)

open("foo", "w").write("foo")
open("bar", "w").write("bar")

os.mkdir("subdir")
os.mkdir("subdir/subsubdir")
open("subdir/foosub1", "w").write("foosub")
open("subdir/foosub2", "w").write("foosub")
open("subdir/subsubdir/foosubsub", "w").write("foosubsub")

@classmethod
def tearDownClass(self):
"""Change back to initial working dir and remove temp test directory. """
os.chdir(self.working_dir)
shutil.rmtree(self.test_dir)

def tearDown(self):
"""Clear the ARTIFACT_EXLCUDES after every test. """
settings.ARTIFACT_EXCLUDES = []
settings.ARTIFACT_BASE_PATH = None

def test_not_existing_base_path(self):
"""Raise exception with not existing base path setting. """
settings.ARTIFACT_BASE_PATH = "path_does_not_exist"
with self.assertRaises(OSError):
record_artifacts_as_dict(["."])

def test_base_path_is_child_dir(self):
"""Test path of recorded artifacts and cd back with child as base."""
settings.ARTIFACT_BASE_PATH = "subdir"
artifacts_dict = record_artifacts_as_dict(["."])
self.assertListEqual(sorted(artifacts_dict.keys()),
sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
self.assertEquals(os.getcwd(), self.test_dir)

def test_base_path_is_parent_dir(self):
"""Test path of recorded artifacts and cd back with parent as base. """
settings.ARTIFACT_BASE_PATH = ".."
os.chdir("subdir/subsubdir")
artifacts_dict = record_artifacts_as_dict(["."])
self.assertListEqual(sorted(artifacts_dict.keys()),
sorted(["foosub1", "foosub2", "subsubdir/foosubsub"]))
self.assertEquals(os.getcwd(),
os.path.join(self.test_dir, "subdir/subsubdir"))
os.chdir(self.test_dir)


class TestInTotoRecordStart(unittest.TestCase):
""""Test in_toto_record_start(step_name, key, material_list). """

Expand Down Expand Up @@ -386,5 +344,5 @@ def test_wrong_signature_in_unfinished_metadata(self):
open(self.link_name, "r")
os.remove(self.link_name_unfinished)

if __name__ == '__main__':
if __name__ == "__main__":
unittest.main()

0 comments on commit 9ba7693

Please sign in to comment.