Skip to content

Commit

Permalink
Add Python script to run Rspec tests in docker container
Browse files Browse the repository at this point in the history
This builds on the Ruby Test Explorer VSCode extension.
See my comment here: connorshea/vscode-ruby-test-adapter#30 (comment)
  • Loading branch information
Splines committed Dec 18, 2023
1 parent 5469dc1 commit bf30e9f
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 3 deletions.
4 changes: 1 addition & 3 deletions .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
--color
--require rails_helper
--format documentation
--require rails_helper
71 changes: 71 additions & 0 deletions spec/rspec_inside_docker.py
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)

0 comments on commit bf30e9f

Please sign in to comment.