Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions pipreqs/pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
--no-follow-links Do not follow symbolic links in the project
--encoding <charset> Use encoding parameter for file open
--savepath <file> Save the list of requirements in the given file
--output <file> Save the list of requirements in the given file, or
use - for standard output
--print Output the list of requirements in the standard
output
--force Overwrite existing requirements.txt
Expand Down Expand Up @@ -510,6 +512,12 @@ def init(args):
scan_noteboooks = args.get("--scan-notebooks", False)
handle_scan_noteboooks()

output_path = args.get("--output")
if output_path == "-":
args["--print"] = True
elif output_path:
args["--savepath"] = output_path

input_path = args["<path>"]

if encoding is None:
Expand Down
55 changes: 55 additions & 0 deletions tests/test_pipreqs.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,61 @@ def test_init_savepath(self):
for item in self.modules2:
self.assertTrue(item.lower() in data)

def test_init_output_path(self):
"""
Test that --output can save requirements.txt to a different path
"""
pipreqs.init(
{
"<path>": self.project,
"--savepath": None,
"--output": self.alt_requirement_path,
"--use-local": None,
"--proxy": None,
"--pypi-server": None,
"--print": False,
"--diff": None,
"--clean": None,
"--mode": None,
}
)
assert os.path.exists(self.alt_requirement_path) == 1
with open(self.alt_requirement_path, "r") as f:
data = f.read().lower()
for item in self.modules[:-3]:
self.assertTrue(item.lower() in data)

def test_init_output_dash(self):
"""
Test that --output - prints requirements to stdout
"""
captured_output = StringIO()
original_stdout = sys.stdout
sys.stdout = captured_output
try:
pipreqs.init(
{
"<path>": self.project,
"--savepath": None,
"--output": "-",
"--print": False,
"--use-local": None,
"--force": None,
"--proxy": None,
"--pypi-server": None,
"--diff": None,
"--clean": None,
"--mode": None,
}
)
finally:
sys.stdout = original_stdout

stdout_content = captured_output.getvalue().lower()
for item in self.modules[:-3]:
self.assertTrue(item.lower() in stdout_content)
self.assertFalse(os.path.exists(self.requirements_path))

def test_init_overwrite(self):
"""
Test that if requiremnts.txt exists, it will not be
Expand Down