SECTION: AbstractDataCommand interface
CLASS AbstractDataCommand:
preferred_suffix: STRING = "" // default
METHOD command(cache_target: STRING) -> STRING
// To be implemented by concrete commands
RAISE NotImplementedError
SECTION: Session context manager
CLASS Session:
METHOD init(require_output: BOOLEAN = TRUE, shell: FUNCTION):
self.require_output = require_output
self.shell = shell // callable accepting a single argument: a value or a tuple
self._cache: MAP[OBJECT, STRING] = EMPTY_MAP // per-session memoization cache: data object -> cache_path
text
METHOD __enter__() -> Session
RETURN self
METHOD __exit__(exc_type, exc, traceback) -> BOOLEAN
// No cleanup required for tests
RETURN FALSE
METHOD save(data: AbstractDataCommand, output_path: STRING):
IF data NOT IN self._cache:
cache_path_input := data.command(output_path) // call exactly once for first save
self.shell(cache_path_input) // initialize cached data
cache_path := cache_path_input
self._cache[data] := cache_path
ELSE:
cache_path := self._cache[data] // reuse cached path, do not call data.command again
self.shell(('cp', '-r', cache_path, output_path)) // copy cached data to requested output path
SECTION: Tests (behavior expectations)
TEST PLAN:
GIVEN a mock_data of type AbstractDataCommand and a mock_shell
WHEN Session.save(mock_data, "pathA") is called for the first time
EXPECT mock_data.command("pathA") called exactly once
EXPECT mock_shell called with the value returned by mock_data.command("pathA")
EXPECT mock_shell called with ('cp', '-r', <cache_path>, 'pathA')
WHEN Session.save(mock_data, "pathB") is called for the second time with the same mock_data
EXPECT mock_data.command(...) NOT called again
EXPECT mock_shell called with ('cp', '-r', <same_cache_path>, 'pathB')
// Note: No real filesystem operations are performed; shell is a mock/stub.