Mocking#
Concourse Tools contains a number of utility functions for mocking various parts of the process for testing purposes.
Environment Variables#
- concoursetools.mocking.mock_environ(new_environ)[source]#
Mock
os.environin a context manager.
- concoursetools.mocking.create_env_vars(one_off_build=False, instance_vars=None, **env_vars)[source]#
Create fake environment variables for a Concourse stage.
- Parameters:
- Example:
>>> for key, value in create_env_vars().items(): ... print(key, value) BUILD_ID 12345678 BUILD_NAME 42 BUILD_TEAM_NAME my-team ATC_EXTERNAL_URL https://ci.myconcourse.com BUILD_JOB_NAME my-job BUILD_PIPELINE_NAME my-pipeline >>> for key, value in create_env_vars(one_off_build=True).items(): ... print(key, value) BUILD_ID 12345678 BUILD_NAME 42 BUILD_TEAM_NAME my-team ATC_EXTERNAL_URL https://ci.myconcourse.com
- Return type:
Input / Output#
- concoursetools.mocking.mock_stdin(stdin)[source]#
Mock
stdinin a context manager.- Parameters:
stdin (
str) – A new string to be used for the stdin.- Example:
>>> with mock_stdin("new_stdin"): ... print(sys.stdin.read()) new_stdin
- Return type:
Warning
As it’s a file object,
sys.stdinis only mocked once by this decorator, and subsequent calls toread()will returnNone.
- class concoursetools.mocking.StringIOWrapper[source]#
A basic wrapper around a
StringIOinstance for capturingstdoutandstderr.An instance of this class acts a bit like a string, to the extent that
==will compare thevalueof the instance to a string.- Example:
Capture
stderrwithcapture_stderr():>>> output = StringIOWrapper() >>> with output.capture_stderr(): ... print("abc") ... print("def", file=sys.stderr) abc >>> output == "def\n" True
Or capture both
stdoutandstderrwithcapture_stdout_and_stderr():>>> output = StringIOWrapper() >>> with output.capture_stdout_and_stderr(): ... print("abc") ... print("def", file=sys.stderr) >>> output == "abc\ndef\n" True
Directory State#
Often you need to mock certain files when testing your resource, which are usually accessible in the resource folders. Rather than set this up manually, you can pass a directory state to TemporaryDirectoryState to make this easier.
- class concoursetools.mocking.TemporaryDirectoryState(starting_state=None, max_depth=2, encoding=None, **kwargs)[source]#
A class representing the state of a temporary directory, to be used as a context manager.
- Parameters:
starting_state (
Optional[Dict[str,Any]]) –The starting state of the directory.Keys of the dictionary are strings (relative to that level, so the name of a folder instead of a full path.) For the values, we have the following:
A file is represented by a string containing the contents of the file. An empty string represents an empty file.
A folder is represented by a dictionary yielding more files and folders.
max_depth (
int) – The maximum depth into which the function can descend. A value of 1 will not enter any subdirectories, a value of 2 will not enter any sub-subdirectories etc.encoding (
Optional[str]) – The encoding to be used to open the files. Will use the system by default when not set.kwargs (
Any) – Keyword arguments to be passed toTemporaryDirectory.
- Example:
>>> folder_state = { ... "folder_1": {}, ... "folder_2": { ... "folder_3": { ... "file_3": "Testing 3", ... }, ... "file_2": "Testing 2", ... }, ... "file_1": "Testing 1", ... } >>> with TemporaryDirectoryState(folder_state) as temp_dir: ... file_2 = temp_dir.path / "folder_2" / "file_2" ... print(file_2.read_text()) Testing 2 >>> file_2.read_text() Traceback (most recent call last): ... FileNotFoundError: [Errno 2] No such file or directory: '.../folder_2/file_2'
- property path: Path#
Return the path to the temporary directory.
- Raises:
RuntimeError – If the temporary directory is currently closed.
- property final_state: Dict[str, Any]#
Return the final state of the directory when it closed.
- Raises:
RuntimeError – If the temporary directory is currently open.