Example #1
0
 def fixture():
     skip_if_no_svn()
     repo_name = 'svnrepo'
     tmpdir = os.path.realpath(tempfile.mkdtemp(prefix='niceman-tests-'))
     root_dir = os.path.join(tmpdir, repo_name)
     subdir = os.path.join(tmpdir, 'subdir')
     os.mkdir(subdir)
     runner = Runner()
     runner.run(['svnadmin', 'create', root_dir])
     runner.run(['svn', 'checkout', 'file://' + root_dir], cwd=subdir)
     checked_out_dir = os.path.join(subdir, repo_name)
     if kind != 'empty':
         runner.run(['touch', 'foo'], cwd=checked_out_dir)
         runner.run(['svn', 'add', 'foo'], cwd=checked_out_dir)
         runner.run(['svn', 'commit', '-m', 'bar'], cwd=checked_out_dir)
     yield (root_dir, checked_out_dir)
     shutil.rmtree(tmpdir)
Example #2
0
def venv_test_dir():
    dirs = AppDirs('niceman')
    test_dir = os.path.join(dirs.user_cache_dir, 'venv_test')
    if os.path.exists(test_dir):
        return test_dir

    runner = Runner()
    runner.run(["mkdir", "-p", test_dir])

    pymod_dir = os.path.join(test_dir, "minimal_pymodule")
    create_pymodule(pymod_dir)

    with chpwd(test_dir):
        runner.run(["virtualenv", "--python", PY_VERSION, "venv0"])
        runner.run(["virtualenv", "--python", PY_VERSION, "venv1"])
        runner.run(["./venv0/bin/pip", "install", "pyyaml"])
        runner.run(["./venv0/bin/pip", "install", "-e", pymod_dir])
        runner.run(["./venv1/bin/pip", "install", "attrs"])
    return test_dir
Example #3
0
def check_run_and_get_output(cmd):
    runner = Runner()
    try:
        # suppress log output happen it was set to high values
        with patch.dict('os.environ', {'NICEMAN_LOGLEVEL': 'WARN'}):
            output = runner.run(["niceman", "--help"])
    except CommandError as e:
        raise AssertionError("'niceman --help' failed to start normally. "
                             "Exited with %d and output %s" % (e.code, (e.stdout, e.stderr)))
    return output
Example #4
0
class ShellSession(POSIXSession):
    """Local shell session"""

    def __init__(self):
        super(ShellSession, self).__init__()
        self._runner = None

    def start(self):
        self._runner = Runner()

    def stop(self):
        self._runner = None

    #
    # Commands fulfilling a "Session" interface to interact with the environment
    #
    def _execute_command(self, command, env=None, cwd=None):
        """
        Execute the given command in the environment.

        Parameters
        ----------
        command : list
            Shell command string or list of command tokens to send to the
            environment to execute.
        env : dict
            Additional (or replacement) environment variables which are applied
            only to the current call

        Returns
        -------
        out, err
        """
        # XXX should it be a generic behavior to auto-start?
        if self._runner is None:
            self.start()
        run_kw = {}
        if env:
            # if anything custom, then we need to get original full environment
            # and update it with custom settings which we either "accumulated"
            # via set_envvar, or it was passed into this call.
            run_kw['env'] = get_updated_env(os.environ, env)

        return self._runner.run(
            command,
            # For now we do not ERROR out whenever command fails or provides
            # stderr -- analysis will be done outside
            expect_fail=True,
            expect_stderr=True,
            cwd=cwd,
            **run_kw
        )  # , shell=True)

    def isdir(self, path):
        return os.path.isdir(path)

    def mkdir(self, path, parents=False):
        if not os.path.exists(path):
            if parents:
                os.makedirs(path)
            else:
                os.mkdir(path)
Example #5
0
class ShellSession(POSIXSession):
    """Local shell session"""
    def __init__(self):
        super(ShellSession, self).__init__()
        self._runner = None

    @borrowdoc(Session)
    def open(self):
        self._runner = Runner()

    @borrowdoc(Session)
    def close(self):
        self._runner = None

    @borrowdoc(Session)
    def _execute_command(self, command, env=None, cwd=None):
        # XXX should it be a generic behavior to auto-start?
        if self._runner is None:
            self.open()
        run_kw = {}
        if env:
            # if anything custom, then we need to get original full environment
            # and update it with custom settings which we either "accumulated"
            # via set_envvar, or it was passed into this call.
            run_kw['env'] = get_updated_env(os.environ, env)

        return self._runner.run(
            command,
            # For now we do not ERROR out whenever command fails or provides
            # stderr -- analysis will be done outside
            expect_fail=True,
            expect_stderr=True,
            cwd=cwd,
            **run_kw)  # , shell=True)

    @borrowdoc(Session)
    def isdir(self, path):
        return os.path.isdir(path)

    @borrowdoc(Session)
    def mkdir(self, path, parents=False):
        if not os.path.exists(path):
            if parents:
                os.makedirs(path)
            else:
                try:
                    os.mkdir(path)
                except OSError:
                    raise CommandError(
                        msg="Failed to make directory {}".format(path))

    @borrowdoc(Session)
    def get(self, src_path, dest_path=None, uid=-1, gid=-1):
        dest_path = self._prepare_dest_path(src_path, dest_path)
        shutil.copy(src_path, dest_path)
        if uid > -1 or gid > -1:
            self.chown(dest_path, uid, gid, recursive=True)

    @borrowdoc(Session)
    def put(self, src_path, dest_path, uid=-1, gid=-1):
        # put is the same as get for the shell resource
        self.get(src_path, dest_path, uid, gid)
Example #6
0
def test_git_repo_remotes(git_repo_pair):
    repo_local, repo_remote = git_repo_pair
    runner = Runner()
    tracer = VCSTracer()

    # Set remote.pushdefault to a value we know doesn't exist.
    # Otherwise, the test machine may have remote.pushdefault globally
    # configured to point to "origin".
    runner.run(["git", "config", "remote.pushdefault", "notexisting"],
               cwd=repo_local)
    # Add another remote that doesn't contain the current commit (in
    # fact doesn't actually exist), so that we test the "listed as
    # remote but doesn't contain" case.
    runner.run(["git", "remote", "add", "fakeremote", "fakepath"],
               cwd=repo_local)

    paths = [os.path.join(repo_local, "foo")]

    dists_nopush = list(tracer.identify_distributions(paths))
    assert_distributions(dists_nopush,
                         expected_length=1,
                         expected_subset={
                             "name":
                             "git",
                             "packages": [{
                                 "files": paths,
                                 "path": repo_local,
                                 "branch": "master",
                                 "tracked_remote": "origin",
                                 "remotes": {
                                     "origin": {
                                         "url": repo_remote,
                                         "contains": True
                                     },
                                     "fakeremote": {
                                         "url": "fakepath"
                                     }
                                 }
                             }]
                         })
    pkg_nopush = dists_nopush[0][0].packages[0]
    assert set(pkg_nopush.remotes.keys()) == {"origin", "fakeremote"}

    # fakeremote, which doesn't contain the current commit, doesn't
    # have contains=True.
    assert "contains" in pkg_nopush.remotes["origin"]
    assert "contains" not in pkg_nopush.remotes["fakeremote"]
    # pushurl is not included in the output above because it is not
    # set.
    assert "pushurl" not in list(pkg_nopush.remotes.values())

    # If we set the pushurl and retrace, it is included.
    runner.run(["git", "config", "remote.origin.pushurl", repo_remote],
               cwd=repo_local)
    dists_push = list(tracer.identify_distributions(paths))
    pkg_push = dists_push[0][0].packages[0]
    assert pkg_push.remotes["origin"]["pushurl"] == repo_remote

    # If we are at a commit that none of the remotes are known to
    # contain, there are no listed remotes.
    with chpwd(repo_local):
        runner(["git", "commit", "--allow-empty", "-m", "empty commit"])

    dists_nocontain = list(tracer.identify_distributions(paths))
    assert not dists_nocontain[0][0].packages[0].remotes.values()

    # The remote repository, however, doesn't have a remote, so there
    # are not listed remotes.
    paths = [os.path.join(repo_remote, "foo")]
    dists_remote = list(tracer.identify_distributions(paths))
    assert not dists_remote[0][0].packages[0].remotes.values()