diff --git a/piptools/scripts/compile.py b/piptools/scripts/compile.py index 5eba0fe43..cd6fe939e 100755 --- a/piptools/scripts/compile.py +++ b/piptools/scripts/compile.py @@ -241,15 +241,15 @@ def cli(verbose, dry_run, pre, rebuild, find_links, index_url, extra_index_url, default_index_url=repository.DEFAULT_INDEX_URL, index_urls=repository.finder.index_urls, trusted_hosts=pip_options.trusted_hosts, - format_control=repository.finder.format_control) + format_control=repository.finder.format_control, + allow_unsafe=allow_unsafe) writer.write(results=results, unsafe_requirements=resolver.unsafe_constraints, reverse_dependencies=reverse_dependencies, primary_packages={key_from_req(ireq.req) for ireq in constraints if not ireq.constraint}, markers={key_from_req(ireq.req): ireq.markers for ireq in constraints if ireq.markers}, - hashes=hashes, - allow_unsafe=allow_unsafe) + hashes=hashes) if dry_run: log.warning('Dry-run, so nothing updated.') diff --git a/piptools/writer.py b/piptools/writer.py index 97b6df941..ccabe124b 100644 --- a/piptools/writer.py +++ b/piptools/writer.py @@ -11,7 +11,8 @@ class OutputWriter(object): def __init__(self, src_files, dst_file, dry_run, emit_header, emit_index, emit_trusted_host, annotate, generate_hashes, - default_index_url, index_urls, trusted_hosts, format_control): + default_index_url, index_urls, trusted_hosts, format_control, + allow_unsafe): self.src_files = src_files self.dst_file = dst_file self.dry_run = dry_run @@ -24,6 +25,7 @@ def __init__(self, src_files, dst_file, dry_run, emit_header, emit_index, self.index_urls = index_urls self.trusted_hosts = trusted_hosts self.format_control = format_control + self.allow_unsafe = allow_unsafe def _sort_key(self, ireq): return (not ireq.editable, str(ireq.req).lower()) @@ -47,6 +49,8 @@ def write_header(self): params += ['--no-annotate'] if self.generate_hashes: params += ["--generate-hashes"] + if self.allow_unsafe: + params += ["--allow-unsafe"] params += ['--output-file', self.dst_file] params += self.src_files yield comment('# pip-compile {}'.format(' '.join(params))) @@ -82,7 +86,7 @@ def write_flags(self): yield '' def _iter_lines(self, results, unsafe_requirements, reverse_dependencies, - primary_packages, markers, hashes, allow_unsafe=False): + primary_packages, markers, hashes): for line in self.write_header(): yield line for line in self.write_flags(): @@ -110,20 +114,20 @@ def _iter_lines(self, results, unsafe_requirements, reverse_dependencies, primary_packages, marker=markers.get(key_from_req(ireq.req)), hashes=hashes) - if not allow_unsafe: + if not self.allow_unsafe: yield comment('# {}'.format(req)) else: yield req def write(self, results, unsafe_requirements, reverse_dependencies, - primary_packages, markers, hashes, allow_unsafe=False): + primary_packages, markers, hashes): with ExitStack() as stack: f = None if not self.dry_run: f = stack.enter_context(AtomicSaver(self.dst_file)) for line in self._iter_lines(results, unsafe_requirements, reverse_dependencies, - primary_packages, markers, hashes, allow_unsafe=allow_unsafe): + primary_packages, markers, hashes): log.info(line) if f: f.write(unstyle(line).encode('utf-8')) diff --git a/tests/test_writer.py b/tests/test_writer.py index 50b00257c..b3b3773e9 100644 --- a/tests/test_writer.py +++ b/tests/test_writer.py @@ -1,4 +1,4 @@ -from pytest import fixture +from pytest import fixture, mark from piptools._compat import FormatControl from piptools.utils import comment @@ -14,7 +14,8 @@ def writer(): generate_hashes=False, default_index_url=None, index_urls=[], trusted_hosts=[], - format_control=FormatControl(set(), set())) + format_control=FormatControl(set(), set()), + allow_unsafe=False) def test_format_requirement_annotation_editable(from_editable, writer): @@ -82,20 +83,38 @@ def test_format_requirement_environment_marker(from_line, writer): 'test ; python_version == "2.7" and platform_python_implementation == "CPython"') -def test_iter_lines__unsafe_dependencies(from_line, writer): +@mark.parametrize(('allow_unsafe',), [(True,), (False,)]) +def test_iter_lines__unsafe_dependencies(from_line, allow_unsafe): + + writer = OutputWriter( + src_files=["src_file", "src_file2"], dst_file="dst_file", + dry_run=True, + emit_header=True, emit_index=True, emit_trusted_host=True, + annotate=True, + generate_hashes=False, + default_index_url=None, index_urls=[], + trusted_hosts=[], + format_control=FormatControl(set(), set()), + allow_unsafe=allow_unsafe, + ) + ireq = [from_line('test==1.2')] unsafe_req = [from_line('setuptools')] reverse_dependencies = {'test': ['xyz']} - lines = writer._iter_lines(ireq, - unsafe_req, - reverse_dependencies, - ['test'], - {}, - None) - str_lines = [] - for line in lines: - str_lines.append(line) + str_lines = list(writer._iter_lines( + ireq, + unsafe_req, + reverse_dependencies, + ['test'], + {}, + None, + )) assert comment('# The following packages are considered to be unsafe in a requirements file:') in str_lines - assert comment('# setuptools') in str_lines + if allow_unsafe: + assert comment('# pip-compile --allow-unsafe --output-file dst_file src_file src_file2') in str_lines + assert 'setuptools' in str_lines + else: + assert comment('# pip-compile --output-file dst_file src_file src_file2') in str_lines + assert comment('# setuptools') in str_lines assert 'test==1.2' in str_lines