def test_log_subprocess_check_output(monkeypatch): recorded = dict(args=(), kwargs=()) def mock_check_output(*args, **kwargs): recorded['args'] = args recorded['kwargs'] = kwargs monkeypatch.setattr('subprocess.check_output', mock_check_output) logged_subprocess.check_output(['a', 'b'], foo='bar') assert recorded == dict(args=(), kwargs=dict(args=['a', 'b'], foo='bar'))
def _git_ignored_files(project_directory, frontend): if not os.path.exists(os.path.join(project_directory, ".git")): return [] # It is pretty involved to parse .gitignore correctly. Lots of # little syntax rules that don't quite match python's fnmatch, # there can be multiple .gitignore, and there are also things # in the git config file that affect what's ignored. So we # let git do this itself. If the project has a `.git` we assume # the user is using git. # --other means show untracked (not added) files # --ignored means show ignored files # --exclude-standard means use the usual .gitignore and other configuration # --directory means output "node_modules/" if it's ignored, not 100000 JS files try: output = logged_subprocess.check_output([ 'git', 'ls-files', '--others', '--ignored', '--exclude-standard', '--directory' ], cwd=project_directory) # for whatever reason, git doesn't include the ".git" in the ignore list return [".git"] + output.decode('utf-8').splitlines() except subprocess.CalledProcessError as e: message = e.output.decode('utf-8').replace("\n", " ") frontend.error("'git ls-files' failed to list ignored files: %s." % (message)) return None except OSError as e: frontend.error("Failed to run 'git ls-files'; %s" % str(e)) return None
def test_log_subprocess_check_output_with_logging(monkeypatch): logger = _test_logger() verbose.push_verbose_logger(logger) try: recorded = dict(args=(), kwargs=()) def mock_check_output(*args, **kwargs): recorded['args'] = args recorded['kwargs'] = kwargs monkeypatch.setattr('subprocess.check_output', mock_check_output) logged_subprocess.check_output(['a', 'b'], foo='bar') assert recorded == dict(args=(), kwargs=dict(args=['a', 'b'], foo='bar')) assert logger.messages == ['$ a b'] finally: verbose.pop_verbose_logger()