diff options
author | Richard Maw <richard.maw@codethink.co.uk> | 2014-02-05 14:31:10 +0000 |
---|---|---|
committer | Richard Maw <richard.maw@codethink.co.uk> | 2014-02-11 11:50:44 +0000 |
commit | 5e4610f0a687f4b308ac1283d4c2ad0b707e42f5 (patch) | |
tree | d8e1ed5acbc8c68049cd24904ef8cb918105d8e5 | |
parent | d80685642ad8d01da34e21f3957df0c06075248a (diff) | |
download | cmdtest-5e4610f0a687f4b308ac1283d4c2ad0b707e42f5.tar.gz |
Factor out shell library loading into yarnlib
-rwxr-xr-x | yarn | 11 | ||||
-rw-r--r-- | yarnlib/__init__.py | 2 | ||||
-rw-r--r-- | yarnlib/shell_libraries.py | 45 | ||||
-rw-r--r-- | yarnlib/shell_libraries_tests.py | 59 |
4 files changed, 109 insertions, 8 deletions
@@ -222,14 +222,9 @@ class YarnRunner(cliapp.Application): self.info('No shell libraries defined') return '' - libs = [] - for filename in self.settings['shell-library']: - self.info('Loading shell library %s' % filename) - with open(filename) as f: - text = f.read() - libs.append('# Loaded from %s\n\n%s\n\n' % (filename, text)) - - return ''.join(libs) + return yarnlib.load_shell_libraries( + self.settings['shell-library'], + pre_read_cb=lambda fn: self.info('Loading shell library %s' % fn)) def select_scenarios(self, scenarios): diff --git a/yarnlib/__init__.py b/yarnlib/__init__.py index f224a0c..b5b207d 100644 --- a/yarnlib/__init__.py +++ b/yarnlib/__init__.py @@ -21,3 +21,5 @@ from elements import Scenario, ScenarioStep, Implementation from block_parser import BlockParser, BlockError from scenario_validator import (NoScenariosError, DuplicateScenariosError, NoThensError, ScenarioValidator) + +from shell_libraries import load_shell_libraries diff --git a/yarnlib/shell_libraries.py b/yarnlib/shell_libraries.py new file mode 100644 index 0000000..3607adc --- /dev/null +++ b/yarnlib/shell_libraries.py @@ -0,0 +1,45 @@ +# Copyright 2014 Lars Wirzenius and Codethink Limited +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +def load_shell_libraries(paths, pre_read_cb=lambda filename: None, open=open): + '''Helper for loading shell libraries from files. + + `paths`: Iterable of file paths to shell libraries. + `pre_read_cb`: Optional callback for providing progress reporting + and logging. + It is given only one parameter, of the path to the + shell library that is about to be loaded. + `open`: Optional function for opening file paths. This is useful to + override how files are opened for unit tests or virtual + file systems. + + This function is provided so that when shell libraries are loaded + by applications that use yarnlib, they can all load them with the + same format, including the comments about where the snippet came from, + and any future additions. + + ''' + + libs = [] + for filename in paths: + pre_read_cb(filename) + with open(filename) as f: + text = f.read() + libs.append('# Loaded from %s\n\n%s\n\n' % (filename, text)) + + return ''.join(libs) diff --git a/yarnlib/shell_libraries_tests.py b/yarnlib/shell_libraries_tests.py new file mode 100644 index 0000000..69e015a --- /dev/null +++ b/yarnlib/shell_libraries_tests.py @@ -0,0 +1,59 @@ +# Copyright 2014 Codethink Limited +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. +# +# =*= License: GPL-3+ =*= + + +import contextlib +import StringIO +import unittest + +import yarnlib + + +class ShellLibrariesTests(unittest.TestCase): + + libfiles = { + 'die.shlib': ''' + die() { + echo "$@" >&2 + return 1 + } + ''', + 'chdir.shlib':''' + chdir() { + ( + cd "$1" + shift + exec "$@" + ) + } + ''', + } + + def fakeopen(self, path, *args, **kwargs): + return contextlib.closing(StringIO.StringIO(self.libfiles[path])) + + def test_file_names_listed(self): + prelude = yarnlib.load_shell_libraries(self.libfiles.keys(), + open=self.fakeopen) + for filename in self.libfiles.iterkeys(): + self.assertIn(filename, prelude) + + def test_all_contents_included(self): + prelude = yarnlib.load_shell_libraries(self.libfiles.keys(), + open=self.fakeopen) + for contents in self.libfiles.itervalues(): + self.assertIn(contents, prelude) |