Skip to content

Commit

Permalink
Support server: list as well, refs #156
Browse files Browse the repository at this point in the history
  • Loading branch information
simonw committed Sep 26, 2024
1 parent 99d1d69 commit 7bcda46
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 6 deletions.
10 changes: 9 additions & 1 deletion docs/multi.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,15 @@ If you need to run a server for the duration of the `shot-scraper multi` session
```yaml
- server: python -m http.server 8000
```
You can now take screenshots of `http://localhost:8000/` and any other URLs that are relative to that server:
The `server:` key also accepts a list of arguments:
```yaml
- server:
- python
- -m
- http.server
- 8000
```
With that server configured, you can now take screenshots of `http://localhost:8000/` and any other URLs hosted by that server:
```yaml
- output: index.png
url: http://localhost:8000/
Expand Down
10 changes: 7 additions & 3 deletions shot_scraper/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -556,9 +556,13 @@ def multi(
subprocess.run([sys.executable, "-c", shot["python"]])
if "server" in shot:
# Start that subprocess and remember the pid
server_processes.append(
subprocess.Popen(shot["server"], shell=True)
)
server = shot["server"]
if isinstance(server, str):
server_processes.append(subprocess.Popen(server, shell=True))
elif isinstance(server, list):
server_processes.append(subprocess.Popen(map(str, server)))
else:
raise click.ClickException("server: must be a string or list")
time.sleep(1)
if "url" in shot:
try:
Expand Down
15 changes: 13 additions & 2 deletions tests/test_shot_scraper.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ def test_version():
output: output.png
""".strip()

SERVER_YAML2 = """
- server:
- python
- -m
- http.server
- 9023
- url: http://localhost:9023/
output: output.png
""".strip()

COMMANDS_YAML = """
- sh: echo "hello world" > index.html
- sh:
Expand All @@ -30,10 +40,11 @@ def test_version():
"""


def test_multi_server():
@pytest.mark.parametrize("yaml", (SERVER_YAML, SERVER_YAML2))
def test_multi_server(yaml):
runner = CliRunner()
with runner.isolated_filesystem():
open("server.yaml", "w").write(SERVER_YAML)
open("server.yaml", "w").write(yaml)
result = runner.invoke(cli, ["multi", "server.yaml"])
assert result.exit_code == 0, result.output
assert pathlib.Path("output.png").exists()
Expand Down

0 comments on commit 7bcda46

Please sign in to comment.