-
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.
Add Python script to run Rspec tests in docker container
This builds on the Ruby Test Explorer VSCode extension. See my comment here: connorshea/vscode-ruby-test-adapter#30 (comment)
- Loading branch information
Showing
2 changed files
with
72 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
--color | ||
--require rails_helper | ||
--format documentation | ||
--require rails_helper |
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,71 @@ | ||
# call this file: rspec_inside_docker.py | ||
# and put it in your ./spec/ folder | ||
|
||
# Specify the following (or alike) in your settings.json | ||
# "rubyTestExplorer.testFramework": "rspec", | ||
# "rubyTestExplorer.rspecCommand": "python3 ./spec/rspec_inside_docker.py", | ||
# "rubyTestExplorer.rspecDirectory": "./spec/", | ||
# "rubyTestExplorer.logpanel": true, | ||
# "rubyTestExplorer.filePattern": ["*_spec.rb"], | ||
|
||
import sys | ||
import subprocess | ||
|
||
# Path where the custom formatter of the Ruby Test explorer extension is memory-mapped | ||
FORMATTER_PATH_IN_DOCKER = "/root/tmp/formatter.rb" | ||
PROJECT_ROOT_FOLDER_NAME = "mampf" | ||
DOCKER_SERVICE_NAME = "mampf" | ||
|
||
|
||
def switch_formatter_path(): | ||
""" | ||
Memory-maps the custom formatter of the Ruby Test explorer extension. | ||
The Ruby Test explorer extension uses a custom formatter to get a specific | ||
output JSON format. The path to the formatter is automatically passed as an | ||
argument to the rspec command by the extension. We need to replace this | ||
path by the path to the memory-mapped formatter in the Docker container. | ||
""" | ||
|
||
formatter_argument_index = None | ||
for i, arg in enumerate(sys.argv): | ||
if arg == '--require': | ||
formatter_argument_index = i | ||
break | ||
|
||
if formatter_argument_index == None: | ||
print('Please specify "--require path/to/custom/formatter.rb"') | ||
sys.exit(1) | ||
|
||
# Switch the path to the custom formatter to the memory-mapped path | ||
formatter_path_on_host = sys.argv[formatter_argument_index + 1] | ||
sys.argv[formatter_argument_index + 1] = FORMATTER_PATH_IN_DOCKER | ||
|
||
return formatter_path_on_host | ||
|
||
|
||
def process_paths(): | ||
for i, arg in enumerate(sys.argv): | ||
if not arg.startswith('./') and "spec" in arg: | ||
sys.argv[i] = replace_absolute_paths_by_relative_paths(sys.argv[i]) | ||
|
||
|
||
def replace_absolute_paths_by_relative_paths(path): | ||
res = path.split(PROJECT_ROOT_FOLDER_NAME + '/')[1] | ||
if not res: | ||
print(f'Path {path}' | ||
f' does not contain string "{PROJECT_ROOT_FOLDER_NAME}/"') | ||
sys.exit(1) | ||
return './' + res # make path relative | ||
|
||
|
||
if __name__ == '__main__': | ||
formatter_path_on_host = switch_formatter_path() | ||
process_paths() | ||
|
||
rspec_args = ' '.join(sys.argv[1:]) | ||
test_command = f'RAILS_ENV=test bundle exec rspec {rspec_args}' | ||
|
||
docker_cmd = f'cd ./docker/run_cypress_tests && docker compose run --rm -T --entrypoint="" -v {formatter_path_on_host}:{FORMATTER_PATH_IN_DOCKER} {DOCKER_SERVICE_NAME} sh -c "{test_command}"' | ||
|
||
subprocess.call(docker_cmd, shell=True) |