-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds testing.docker module to test docker tutorials
- Loading branch information
1 parent
102f722
commit c1f4384
Showing
5 changed files
with
196 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Testing rst files that call "docker run" | ||
""" | ||
from pathlib import Path | ||
|
||
from pkgmt.testing import rst | ||
|
||
_template = """\ | ||
# exit on error and print each command | ||
set -e | ||
set -x | ||
ROOT=$(pwd) | ||
{code}\ | ||
""" | ||
|
||
|
||
# NOTE: should I modify the command to have the --rm flag? | ||
def _find_docker_run_idx(snippets): | ||
for i, snippet in enumerate(snippets): | ||
if 'docker run' in snippet: | ||
return i | ||
|
||
raise ValueError('Did not find "docker run" command') | ||
|
||
|
||
def _patch_docker_run(snippet): | ||
lines = snippet.splitlines() | ||
idx = None | ||
|
||
for i, line in enumerate(lines): | ||
if line.startswith('docker run'): | ||
idx = i | ||
|
||
if idx is None: | ||
raise ValueError('Failed to find a line starting with ' | ||
f'"docker run", got lines: {lines}') | ||
|
||
docker_run_line = lines[idx] | ||
|
||
if '-t' not in docker_run_line: | ||
raise ValueError('Expected -t flag to be in the "docker run"' | ||
f' command, got: {docker_run_line!r}') | ||
|
||
# cd to $ROOT because the script may have some "cd" commands, and | ||
# we're saving run-in-docker.sh in the initial working directory | ||
lines[idx] = ('cd $ROOT && cat run-in-docker.sh | ' + | ||
docker_run_line.replace('-t ', '')) | ||
|
||
return '\n'.join(lines) | ||
|
||
|
||
def to_script(text): | ||
snippets = rst.to_snippets(text) | ||
|
||
idx = _find_docker_run_idx(snippets) | ||
|
||
pre, post = snippets[:idx + 1], snippets[idx + 1:] | ||
|
||
return pre, post | ||
|
||
|
||
def to_testing_files(text): | ||
pre, post = to_script(text) | ||
pre[-1] = _patch_docker_run(pre[-1]) | ||
|
||
Path('test.sh').write_text(_template.format(code='\n\n'.join(pre))) | ||
Path('run-in-docker.sh').write_text( | ||
_template.format(code='\n\n'.join(post))) | ||
|
||
|
||
def to_testing_files_from_path(path): | ||
to_testing_files(Path(path).read_text()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
from pathlib import Path | ||
|
||
import pytest | ||
|
||
from pkgmt.testing import docker | ||
|
||
simple_in = """\ | ||
Some text | ||
.. code-block:: bash | ||
git clone some-repository | ||
cd some-repository | ||
.. code-block:: bash | ||
docker run -i -t some-image /bin/bash | ||
.. code-block:: sh | ||
echo 'hello from docker' | ||
""" | ||
|
||
simple_test = """\ | ||
# exit on error and print each command | ||
set -e | ||
set -x | ||
ROOT=$(pwd) | ||
git clone some-repository | ||
cd some-repository | ||
cd $ROOT && cat run-in-docker.sh | docker run -i some-image /bin/bash\ | ||
""" | ||
|
||
simple_run_in_docker = """\ | ||
# exit on error and print each command | ||
set -e | ||
set -x | ||
ROOT=$(pwd) | ||
echo 'hello from docker'\ | ||
""" | ||
|
||
multicommand_in = """\ | ||
.. code-block:: bash | ||
git clone some-repository | ||
cd some-repository | ||
.. code-block:: bash | ||
echo hello | ||
docker run -i -t some-image /bin/bash | ||
.. code-block:: sh | ||
echo 'hello from docker' | ||
""" | ||
|
||
multicommand_test = """\ | ||
# exit on error and print each command | ||
set -e | ||
set -x | ||
ROOT=$(pwd) | ||
git clone some-repository | ||
cd some-repository | ||
echo hello | ||
cd $ROOT && cat run-in-docker.sh | docker run -i some-image /bin/bash\ | ||
""" | ||
|
||
multicommand_run_in_docker = """\ | ||
# exit on error and print each command | ||
set -e | ||
set -x | ||
ROOT=$(pwd) | ||
echo 'hello from docker'\ | ||
""" | ||
|
||
|
||
def test_to_script(): | ||
pre, post = docker.to_script(simple_in) | ||
assert pre == [ | ||
'git clone some-repository\ncd some-repository', | ||
'docker run -i -t some-image /bin/bash', | ||
] | ||
assert post == ["echo 'hello from docker'"] | ||
|
||
|
||
@pytest.mark.parametrize('in_, test, run_in_docker', [ | ||
[simple_in, simple_test, simple_run_in_docker], | ||
[multicommand_in, multicommand_test, multicommand_run_in_docker], | ||
]) | ||
def test_to_testing_files(tmp_empty, in_, test, run_in_docker): | ||
docker.to_testing_files(in_) | ||
|
||
test_out = Path('test.sh').read_text() | ||
run_in_docker_out = Path('run-in-docker.sh').read_text() | ||
|
||
assert test_out == test | ||
assert run_in_docker_out == run_in_docker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters