コード例 #1
0
    def test_no_apply_to_directories_not_matching(self):
        """Don't apply to directories."""
        with testutil.in_tempdir(os.getcwd(), "file_patterns") as temp_dir:
            with testutil.in_tempdir(os.getcwd(), "matched"):
                function_applied = Mock()
                util.apply_to_directories(function_applied,
                                          temp_dir,
                                          matching=["{0}/*".format("other")])

                function_applied.assert_not_called()  # suppress(PYC70)
コード例 #2
0
    def test_apply_to_directories_matching(self):
        """Apply functions to directories matching prefix."""
        with testutil.in_tempdir(os.getcwd(), "file_patterns") as temp_dir:
            with testutil.in_tempdir(os.getcwd(), "matched") as matched:
                base = os.path.basename(matched)
                function_applied = Mock()
                util.apply_to_directories(function_applied,
                                          temp_dir,
                                          matching=["*/{0}".format(base)])

                function_applied.assert_called_with(matched)
コード例 #3
0
    def test_create_named_cache_dir(self):
        """Created named cache directory exists."""
        with testutil.in_tempdir(os.getcwd(), "container_dir_test"):
            with removable_container_dir("container") as container:
                cache_dir = container.named_cache_dir("name")

                self.assertThat(cache_dir, DirExists())
コード例 #4
0
    def test_create_language_dir(self):
        """Created language directory exists."""
        with testutil.in_tempdir(os.getcwd(), "container_dir_test"):
            with removable_container_dir("container") as container:
                language_dir = container.language_dir("name")

                self.assertThat(language_dir, DirExists())
コード例 #5
0
    def test_create_dir_and_pass_control_to_downloaded_script(self):
        """Test creating container and passing control to a fetched script."""
        with testutil.server_in_tempdir(os.getcwd(), "server") as server:
            util_script = os.path.join(server[0], "util.py")
            with bootstrap.open_and_force_mkdir(util_script, "w") as f:
                f.write("")
                f.flush()

            setup_script = os.path.join(server[0], "setup/test/setup.py")
            with bootstrap.open_and_force_mkdir(setup_script, "w") as f:
                # Write a simple script to our setup file
                f.write("def run(cont, util, sh, argv):\n"
                        "  print(\"Hello\")\n")
                f.flush()

                dns_overrides = {
                    "public-travis-scripts.polysquare.org": server[1]
                }

            with testutil.overridden_dns(dns_overrides):
                with testutil.in_tempdir(os.getcwd(), "container"):
                    captured_output = testutil.CapturedOutput()

                    with captured_output:
                        bootstrap.main(["-d",
                                        self._container_dir,
                                        "-s",
                                        "setup/test/setup.py",
                                        "--keep-scripts"])

                    self.assertEqual(captured_output.stdout, "Hello\n\n")
コード例 #6
0
 def test_container_dir_has_subdir(self, subdir):
     """Test ContainerDir has subdir."""
     with testutil.in_tempdir(os.getcwd(), "container_dir_test"):
         with removable_container_dir("container"):
             self.assertThat(os.path.join(os.getcwd(),
                                          "container",
                                          subdir),
                             DirExists())
コード例 #7
0
    def test_open_file_in_forced_mkdir(self):
        """Test creating and opening files inside forced multi-layer dirs."""
        with testutil.in_tempdir(os.getcwd(), "force_mkdir_test"):
            with bootstrap.open_and_force_mkdir("forced/mkdir/file", "w") as f:
                f.write("")

            self.assertThat(os.path.join(os.getcwd(), "forced/mkdir/file"),
                            FileExists())
コード例 #8
0
    def test_open_file_in_forced_mkdir_write(self):
        """Test creating and writing to files inside multi-layer dirs."""
        with testutil.in_tempdir(os.getcwd(), "force_mkdir_test"):
            with bootstrap.open_and_force_mkdir("forced/mkdir/file", "w") as f:
                f.write("Contents")

            self.assertThat(os.path.join(os.getcwd(), "forced/mkdir/file"),
                            FileContains("Contents"))
コード例 #9
0
    def test_two_mtimes_have_different_values(self):
        """Two stored modification times have different values."""
        with testutil.in_tempdir(os.getcwd(), "mtimes"):
            util.store_current_mtime_in("1")
            time.sleep(1)
            util.store_current_mtime_in("2")

            self.assertThat(util.fetch_mtime_from("2"),
                            GreaterThan(util.fetch_mtime_from("1")))
コード例 #10
0
    def test_no_act_where_file_doesnt_exist(self):
        """Don't perform action when candidate file doesn't exist."""
        with testutil.in_tempdir(os.getcwd(), "mtimes"):
            callee = Mock()
            util.where_more_recent(PrepopulatedMTimeContainer(None),
                                   os.path.join(os.getcwd(), "temp"),
                                   util.fetch_mtime_from("1"),
                                   callee)

            self.assertEqual(callee.call_args_list, list())
コード例 #11
0
 def test_named_cache_dir_is_subdir_of_cache_dir(self):
     """Created named cache directory is a subdir of container cache."""
     with testutil.in_tempdir(os.getcwd(),
                              "container_dir_test") as temp_dir:
         with removable_container_dir("container") as container:
             cache_dir = container.named_cache_dir("name")
             self.assertEqual(os.path.join(temp_dir,
                                           "container",
                                           "_cache",
                                           "name"),
                              cache_dir)
コード例 #12
0
 def test_named_language_dir_is_subdir_of_languages_dir(self):
     """Created language dir is a subdir of container languages dir."""
     with testutil.in_tempdir(os.getcwd(),
                              "container_dir_test") as temp_dir:
         with removable_container_dir("container") as container:
             language_dir = container.language_dir("name")
             self.assertEqual(os.path.join(temp_dir,
                                           "container",
                                           "_languages",
                                           "name"),
                              language_dir)
コード例 #13
0
 def test_temp_cache_dir_is_subdir_of_cache_dir(self):
     """Created temporary cache directory is a subdir of container cache."""
     with testutil.in_tempdir(os.getcwd(),
                              "container_dir_test") as temp_dir:
         with removable_container_dir("container") as container:
             with container.in_temp_cache_dir() as cache_dir:
                 self.assertEqual(os.path.join(temp_dir,
                                               "container",
                                               "_cache",
                                               os.path.basename(cache_dir)),
                                  cache_dir)
コード例 #14
0
    def test_apply_to_matching_files_by_prefix(self):
        """Apply functions to files matching prefix."""
        with testutil.in_tempdir(os.getcwd(), "file_patterns") as temp_dir:
            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir) as temp_file:
                temp_file.write("")
                temp_file.flush()

                function_applied = Mock()
                util.apply_to_files(function_applied,
                                    temp_dir,
                                    matching=["{0}/*".format(temp_dir)])

                function_applied.assert_called_with(temp_file.name)
コード例 #15
0
    def test_non_executable_file_not_found(self):
        """Don't find a non executable file in the current PATH."""
        if platform.system() == "Windows":
            self.skipTest("no such thing as execute permission on Windows")

        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            os.environ["PATH"] = (temp_dir + os.pathsep)

            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir) as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")

                self.assertEqual(None,
                                 util.which(os.path.basename(temp_file.name)))
コード例 #16
0
    def test_act_on_no_mtime_file(self):
        """Perform action when modification time file doesn't exist."""
        with testutil.in_tempdir(os.getcwd(), "mtimes"):
            callee = Mock()
            with open("temporary_file", "w") as temporary_file:
                temporary_file.write("contents")
                temporary_file.flush()

            container = PrepopulatedMTimeContainer(temporary_file.name)
            util.where_more_recent(container,
                                   temporary_file.name,
                                   util.fetch_mtime_from("1"),
                                   callee)

            self.assertThat(callee.call_args_list, Not(Equals(list())))
コード例 #17
0
    def test_ignore_shebang_when_in_pathext(self):
        """Ignore shebang when extension is in PATHEXT."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            os.environ["PATH"] = (temp_dir +
                                  os.pathsep +
                                  (os.environ.get("PATH") or ""))
            os.environ["PATHEXT"] = ".py"

            with open(os.path.join(temp_dir, "script.py"), "wt") as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")

            os.chmod(temp_file.name, 755)

            cmdline = util.process_shebang([temp_file.name])
            self.assertEqual(cmdline, [temp_file.name])
コード例 #18
0
    def test_no_apply_files_not_matching_suffix(self):
        """Don't apply functions to files not matching suffix."""
        with testutil.in_tempdir(os.getcwd(), "file_patterns") as temp_dir:
            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir,
                                             suffix=".tmp") as temp_file:
                temp_file.write("")
                temp_file.flush()

                function_applied = Mock()
                util.apply_to_files(function_applied,
                                    temp_dir,
                                    matching=["*.{0}".format("other")])

                function_applied.assert_not_called()  # suppress(PYC70)
コード例 #19
0
    def test_apply_to_matching_files_by_multiple_suffixes(self):
        """Apply functions to files matching suffix."""
        with testutil.in_tempdir(os.getcwd(), "file_patterns") as temp_dir:
            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir,
                                             suffix=".tmp2") as temp_file:
                temp_file.write("")
                temp_file.flush()

                function_applied = Mock()
                util.apply_to_files(function_applied,
                                    temp_dir,
                                    matching=["*.tmp1",
                                              "*.tmp2"])

                function_applied.assert_called_with(temp_file.name)
コード例 #20
0
    def test_fetch_modules_and_import(self, module):
        """Test importing as a fetched module."""
        with testutil.in_tempdir(os.getcwd(), "container_dir_test"):
            with testutil.server_in_tempdir(os.getcwd(), "server") as server:
                module_path = os.path.join(server[0], module)
                with bootstrap.open_and_force_mkdir(module_path, "w") as mfile:
                    mfile.write("CONSTANT = 1")
                    mfile.flush()

                with removable_container_dir("container") as container:
                    # Now import the file and make use of it
                    domain = server[1]
                    imported_module = container.fetch_and_import(module,
                                                                 domain)
                    self.note_loaded_module_path(container, module, domain)
                    self.assertEqual(1, imported_module.CONSTANT)
コード例 #21
0
    def test_import_existing_module_in_scripts(self, module):
        """Test importing as a module in _scripts/."""
        with testutil.in_tempdir(os.getcwd(), "container_dir_test"):
            with removable_container_dir("container") as container:
                loadable = os.path.join(os.getcwd(), "container", "_scripts")
                module_path = os.path.join(loadable,
                                           "ciscripts",
                                           module)
                with make_loadable_module_path(module_path, loadable) as f:
                    f.write("CONSTANT = 1")
                    f.flush()

                    # Now import the file and make use of it
                    imported_module = container.fetch_and_import(module)
                    self.note_loaded_module_path(container, module)
                    self.assertEqual(1, imported_module.CONSTANT)
コード例 #22
0
    def test_find_executable_file_in_path(self):
        """Find an executable file in the current PATH."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            os.environ["PATH"] = (temp_dir +
                                  os.pathsep +
                                  (os.environ.get("PATH") or ""))

            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir) as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")
                os.chmod(temp_file.name,
                         os.stat(temp_file.name).st_mode | stat.S_IRWXU)

                which_result = util.which(os.path.basename(temp_file.name))
                self.assertEqual(temp_file.name.lower(),
                                 which_result.lower())
コード例 #23
0
    def test_no_execute_function_if_found_by_which(self):
        """where_unavailable doesn't execute function if executable found."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir) as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")
                os.chmod(temp_file.name, 755)

                with testutil.environment_copy():
                    os.environ["PATH"] = (os.environ["PATH"] +
                                          os.pathsep +
                                          temp_dir)
                    mock = Mock()
                    util.where_unavailable(os.path.basename(temp_file.name),
                                           mock,
                                           "arg")
                    self.assertEquals(mock.call_args_list, list())
コード例 #24
0
    def test_process_shebang(self):
        """Explicitly specify interpreter when file has shebang."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            os.environ["PATH"] = (temp_dir +
                                  os.pathsep +
                                  (os.environ.get("PATH") or ""))

            with open(os.path.join(temp_dir, "script"), "wt") as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")

            os.chmod(temp_file.name,
                     os.stat(temp_file.name).st_mode | stat.S_IRWXU)

            cmdline = util.process_shebang([temp_file.name])
            self.assertEqual(cmdline,
                             [_full_path_if_exists("/usr/bin/env"),
                              "python",
                              temp_file.name])
コード例 #25
0
    def test_resolve_relative_paths(self):
        """Resolve relative paths in PATH."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            path_var = (os.environ.get("PATH") or "")
            base = os.path.basename(temp_dir)
            os.environ["PATH"] = "{0}/../{1}{2}{3}".format(temp_dir,
                                                           base,
                                                           os.pathsep,
                                                           path_var)

            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir) as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")
                os.chmod(temp_file.name, 755)

                which_result = util.which(os.path.basename(temp_file.name))
                self.assertEqual(temp_file.name.lower(),
                                 which_result.lower())
コード例 #26
0
    def test_find_executable_file_using_pathext(self):
        """Find an executable file using PATHEXT."""
        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            os.environ["PATH"] = (temp_dir +
                                  os.pathsep +
                                  (os.environ.get("PATH") or ""))
            os.environ["PATHEXT"] = ".exe"

            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=temp_dir,
                                             suffix=".exe") as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")
                os.chmod(temp_file.name, 755)

                name = os.path.splitext(os.path.basename(temp_file.name))[0]

                which_result = util.which(name)
                self.assertEqual(temp_file.name.lower(),
                                 which_result.lower())
コード例 #27
0
    def test_no_act_where_file_is_up_to_date(self):
        """Don't perform action when candidate file is up to date."""
        with testutil.in_tempdir(os.getcwd(), "mtimes"):
            callee = Mock()
            with open("temporary_file", "w") as temporary_file:
                temporary_file.write("contents")
                temporary_file.flush()

            container = PrepopulatedMTimeContainer(temporary_file.name)

            time.sleep(1)
            util.store_current_mtime_in("1")

            util.where_more_recent(container,
                                   temporary_file.name,
                                   util.fetch_mtime_from("1"),
                                   callee)

            self.assertEqual(callee.call_args_list, list())
コード例 #28
0
    def test_symlinks_in_path_get_resolved(self):
        """Returned executable path has symlinks resolved."""
        if platform.system() == "Windows":
            self.skipTest("symlinks not supported on Windows")

        with testutil.in_tempdir(os.getcwd(), "executable_path") as temp_dir:
            link = os.path.join(temp_dir, "link")
            linked = os.path.join(temp_dir, "linked")

            os.mkdir(linked)
            os.symlink(linked, link)

            path_var = (os.environ.get("PATH") or "")

            os.environ["PATH"] = link + os.pathsep + path_var

            with tempfile.NamedTemporaryFile(mode="wt",
                                             dir=linked) as temp_file:
                temp_file.write("#!/usr/bin/env python\nprint(\"Test\")")
                os.chmod(temp_file.name, 755)

                self.assertEqual(temp_file.name,
                                 util.which(os.path.basename(temp_file.name)))
コード例 #29
0
 def test_force_multiple_layered_mkdir(self):
     """Test forcing creation of multi-layer directory."""
     with testutil.in_tempdir(os.getcwd(), "force_mkdir_test"):
         bootstrap.force_mkdir("forced/mkdir")
         self.assertThat(os.path.join(os.getcwd(), "forced/mkdir"),
                         DirExists())
コード例 #30
0
 def test_force_single_mkdir(self):
     """Test forcing creation of a single directory (normal case)."""
     with testutil.in_tempdir(os.getcwd(), "force_mkdir_test"):
         directory = bootstrap.force_mkdir("forced_mkdir")
         self.assertThat(directory, DirExists())