Skip to content
This repository has been archived by the owner on May 8, 2023. It is now read-only.

Commit

Permalink
Merge pull request #48 from scikit-build/support-env-file-update-from…
Browse files Browse the repository at this point in the history
…-step

ci/driver: Support environment file `env.json` update from within step.
  • Loading branch information
jcfr committed Mar 26, 2019
2 parents f92530d + 6475e12 commit 57a5a33
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 2 deletions.
2 changes: 2 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ details, see the commit logs at http://github.com/scikit-build/scikit-ci
Next Release
============

* Support environment file `env.json` update from within step.

Scikit-ci 0.19.0
================

Expand Down
11 changes: 9 additions & 2 deletions ci/driver.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,19 @@ def __init__(self, driver, env_file="env.json"):

def __enter__(self):
self.driver.load_env(self.env_file)
self.env_file_modified_time = 0
if os.path.exists(self.env_file):
self.env_file_modified_time = os.path.getmtime(self.env_file)
return self.driver

def __exit__(self, exc_type, exc_value, traceback):
if exc_type is None and exc_value is None and traceback is None:
self.driver.save_env(self.driver.env, self.env_file)

# Skip saving if env_file has been modified since context was entered
current_env_file_modified_time = 0
if os.path.exists(self.env_file):
current_env_file_modified_time = os.path.getmtime(self.env_file)
if current_env_file_modified_time == self.env_file_modified_time:
self.driver.save_env(self.driver.env, self.env_file)
self.driver.unload_env()


Expand Down
29 changes: 29 additions & 0 deletions tests/test_scikit_ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from ruamel.yaml.compat import ordereddict

import ci
from . import captured_lines, display_captured_text, push_dir, push_env
from ci.constants import SERVICES, SERVICES_ENV_VAR, STEPS
from ci.driver import Driver, dependent_steps, execute_step
Expand Down Expand Up @@ -1203,3 +1204,31 @@ def test_issue39_propagate_command_script_error(tmpdir):
SKCIStepExecutionError,
message=".*Return code: 2.+Command:.+$(import foo; print(foo())).*"):
execute_step("test", with_dependencies=False)


def test_env_file_update_from_step(tmpdir):
tmpdir.join('scikit-ci.yml').write(textwrap.dedent(
r"""
schema_version: "{version}"
test:
commands:
# The following command is expected to fail
- python: |
import os
os.environ['SCIKIT_CI_UPDATE_FROM_STEP'] = '1'
import sys
sys.path.insert(0, "{ci_path}")
import ci
ci.driver.Driver.save_env(os.environ)
"""
).format(version=SCHEMA_VERSION, ci_path=os.path.join(os.path.dirname(ci.__file__), "..")))

service = 'circle'

environment = dict(os.environ)
enable_service(service, environment)

with push_dir(str(tmpdir)), push_env(**environment):
execute_step("test")
env = Driver.read_env()
assert env['SCIKIT_CI_UPDATE_FROM_STEP'] == '1'

0 comments on commit 57a5a33

Please sign in to comment.