diff --git a/README.md b/README.md index 4930d2a..56f9fe5 100644 --- a/README.md +++ b/README.md @@ -11,6 +11,8 @@ This repository contains the following GSLab Python libraries: Information about each of these packages is available in its internal documentation. +Note: In order to run the unit tests, `mock` (version 2.0.0 or higher) needs to be installed. + Installation ------------ To install this repository's Python libraries, run the `setup.py` script at its root diff --git a/gslab_scons/builders/build_python.py b/gslab_scons/builders/build_python.py index 5c16160..520e883 100644 --- a/gslab_scons/builders/build_python.py +++ b/gslab_scons/builders/build_python.py @@ -24,8 +24,9 @@ def build_python(target, source, env): target_dir = os.path.dirname(target_file) misc.check_code_extension(source_file, 'python') log_file = target_dir + '/sconscript.log' + cl_arg = misc.command_line_arg(env) - os.system('python %s > %s' % (source_file, log_file)) + os.system('python %s %s > %s' % (source_file, cl_arg, log_file)) end_time = misc.current_time() log_timestamp(start_time, end_time, log_file) diff --git a/gslab_scons/builders/build_r.py b/gslab_scons/builders/build_r.py index 805e91b..f7b288e 100644 --- a/gslab_scons/builders/build_r.py +++ b/gslab_scons/builders/build_r.py @@ -24,8 +24,11 @@ def build_r(target, source, env): target_dir = os.path.dirname(target_file) misc.check_code_extension(source_file, 'r') log_file = target_dir + '/sconscript.log' - - os.system('R CMD BATCH --no-save %s %s' % (source_file, log_file)) + + cl_arg = misc.command_line_arg(env) + if cl_arg != '': + cl_arg = "'--args %s'" % cl_arg + os.system("R CMD BATCH --no-save %s %s %s" % (cl_arg, source_file, log_file)) end_time = misc.current_time() log_timestamp(start_time, end_time, log_file) diff --git a/gslab_scons/builders/build_stata.py b/gslab_scons/builders/build_stata.py index ad9c242..7449e96 100644 --- a/gslab_scons/builders/build_stata.py +++ b/gslab_scons/builders/build_stata.py @@ -24,7 +24,8 @@ def build_stata(target, source, env): (By default, SCons will try to find each flavour). ''' start_time = misc.current_time() - + cl_arg = misc.command_line_arg(env) + source = misc.make_list_if_string(source) target = misc.make_list_if_string(target) source_file = str(source[0]) @@ -38,15 +39,15 @@ def build_stata(target, source, env): user_flavor = env['user_flavor'] if user_flavor is not None: if misc.is_unix(): - command = misc.stata_command_unix(user_flavor) + command = misc.stata_command_unix(user_flavor, cl_arg) elif platform == 'win32': - command = misc.stata_command_win(user_flavor) + command = misc.stata_command_win(user_flavor, cl_arg) else: flavors = ['stata-mp', 'stata-se', 'stata'] if misc.is_unix(): for flavor in flavors: if misc.is_in_path(flavor): - command = misc.stata_command_unix(flavor) + command = misc.stata_command_unix(flavor, cl_arg) break elif platform == 'win32': try: @@ -58,7 +59,7 @@ def build_stata(target, source, env): flavors = [f.replace('.exe', '-64.exe') for f in flavors] for flavor in flavors: if misc.is_in_path(flavor): - command = misc.stata_command_win(flavor) + command = misc.stata_command_win(flavor, cl_arg) break try: diff --git a/gslab_scons/misc.py b/gslab_scons/misc.py index 9dc7e8a..9ed42ff 100644 --- a/gslab_scons/misc.py +++ b/gslab_scons/misc.py @@ -20,8 +20,14 @@ def check_lfs(): raise LFSError('''Either Git LFS is not installed or your Git LFS settings need to be updated. Please install Git LFS or run 'git lfs install --force' if prompted above.''') +def command_line_arg(env): + try: + cl_arg = env['CL_ARG'] + except KeyError: + cl_arg = '' + return cl_arg -def stata_command_unix(flavor): +def stata_command_unix(flavor, cl_arg): ''' This function returns the appropriate Stata command for a user's Unix platform. @@ -30,16 +36,16 @@ def stata_command_unix(flavor): 'linux' : '-b', 'linux2': '-b'} option = options[platform] - command = flavor + ' ' + option + ' %s ' # %s will take filename later + command = flavor + ' ' + option + ' %s ' + cl_arg # %s will take filename later return command -def stata_command_win(flavor): +def stata_command_win(flavor, cl_arg): ''' This function returns the appropriate Stata command for a user's Windows platform. ''' - command = flavor + ' /e do' + ' %s ' # %s will take filename later + command = flavor + ' /e do' + ' %s ' + cl_arg # %s will take filename later return command diff --git a/gslab_scons/tests/input/R_test_script.R b/gslab_scons/tests/input/R_test_script.R index 2ab6812..38e6f95 100644 --- a/gslab_scons/tests/input/R_test_script.R +++ b/gslab_scons/tests/input/R_test_script.R @@ -1,3 +1,6 @@ +args = commandArgs(trailingOnly = TRUE) +cat(args[1]) + NCols=5 NRows=5 x<-matrix(runif(NCols*NRows), ncol=NCols) diff --git a/gslab_scons/tests/input/python_test_script.py b/gslab_scons/tests/input/python_test_script.py index 11b2336..64810d1 100644 --- a/gslab_scons/tests/input/python_test_script.py +++ b/gslab_scons/tests/input/python_test_script.py @@ -1,12 +1,13 @@ #! /usr/bin/env python -import argparse, pdb +import sys -parser = argparse.ArgumentParser() -parser.add_argument('-i', dest='input', nargs='+') -args = parser.parse_args() - -if args.input: - message = args.input +try: + arg = sys.argv[1] +except: + arg = '' + +if arg != '': + message = arg else: message = 'Test Output \n' output_name = 'output.txt' diff --git a/gslab_scons/tests/input/stata_test_script.do b/gslab_scons/tests/input/stata_test_script.do index 7ab7a2b..087cf11 100644 --- a/gslab_scons/tests/input/stata_test_script.do +++ b/gslab_scons/tests/input/stata_test_script.do @@ -6,7 +6,9 @@ preliminaries set obs 10 gen var1 = _n -save_data ../output/stata1.dta, key(var1) log(../output/data_file_manifest.log) replace +di `1` + +save ../output/stata`1`.dta, replace save_data ../output/stata2.dta, key(var1) log(../output/data_file_manifest.log) replace save_data ../output/stata.csv, key(var1) outsheet log(../output/data_file_manifest.log) replace save_data ../output/stata.txt, key(var1) outsheet log(../output/data_file_manifest.log) replace diff --git a/gslab_scons/tests/log/make.log b/gslab_scons/tests/log/make.log index 39ed486..93757e4 100644 --- a/gslab_scons/tests/log/make.log +++ b/gslab_scons/tests/log/make.log @@ -2,14 +2,17 @@ test_bad_extension (test_build_lyx.test_build_lyx) Test that build_lyx() recognises an inappropriate file extension ... ok test_default (test_build_lyx.test_build_lyx) ... ok test_bad_extension (test_build_python.testbuild_python) ... ok +test_clarg (test_build_python.testbuild_python) ... ok test_default (test_build_python.testbuild_python) ... ok test_bad_extension (test_build_r.testbuild_r) Test that build_r() recognises an inappropriate file extension ... ok +test_clarg (test_build_r.testbuild_r) ... ok test_default (test_build_r.testbuild_r) ... ok test_bad_extension (test_build_stata.testbuild_stata) ... ok test_bad_user_executable (test_build_stata.testbuild_stata) ... ok +test_clarg (test_build_stata.testbuild_stata) ... ok test_default (test_build_stata.testbuild_stata) ... ok -test_user_executable_unix (test_build_stata.testbuild_stata) ... skipped 'skipped test_user_executable_unix because on a windows machine' +test_user_executable_unix (test_build_stata.testbuild_stata) ... ok test_default (test_build_tables.test_build_tables) ... ok test_default_string_target (test_build_tables.test_build_tables) ... ok test_target_extension (test_build_tables.test_build_tables) @@ -20,11 +23,12 @@ test_start_log (test_log.test_log) ... ok test_check_code_extension (test_misc.test_misc) Unit tests for check_code_extension() ... ok test_check_lfs (test_misc.test_misc) ... ok +test_command_line_arg (test_misc.test_misc) ... ok test_current_time (test_misc.test_misc) Test that current_time() prints times in the expected format ... ok test_is_64_windows (test_misc.test_misc) ... ok -test_is_exe (test_misc.test_misc) ... skipped 'skipped test_is_exe because on a windows machine' -test_is_in_path (test_misc.test_misc) ... skipped 'skipped test_is_in_path because on a windows machine' +test_is_exe (test_misc.test_misc) ... ok +test_is_in_path (test_misc.test_misc) ... ok test_is_unix (test_misc.test_misc) ... ok test_make_list_if_string (test_misc.test_misc) ... ok test_stata_command_unix (test_misc.test_misc) ... ok @@ -40,6 +44,6 @@ test_upload_asset_bad_file (test_release_tools.TestReleaseTools) ... ok test_upload_asset_standard (test_release_tools.TestReleaseTools) ... ok ---------------------------------------------------------------------- -Ran 34 tests in 24.886s +Ran 38 tests in 55.880s -OK (skipped=4) +OK (skipped=1) diff --git a/gslab_scons/tests/test_build_python.py b/gslab_scons/tests/test_build_python.py index d6410e1..a762b1e 100644 --- a/gslab_scons/tests/test_build_python.py +++ b/gslab_scons/tests/test_build_python.py @@ -16,15 +16,23 @@ def setUp(self): os.mkdir('../build/') def test_default(self): - env = '' + env = {} build_python('../build/py.py', './input/python_test_script.py', env) logfile_data = open('../build/sconscript.log', 'rU').read() self.assertIn('Log created:', logfile_data) if os.path.isfile('../build/sconscript.log'): os.remove('../build/sconscript.log') - + + def test_clarg(self): + env = {'CL_ARG' : 'COMMANDLINE'} + build_python('../build/py.py', './input/python_test_script.py', env) + logfile_data = open('output.txt', 'rU').read() + self.assertIn('COMMANDLINE', logfile_data) + if os.path.isfile('../build/sconscript.log'): + os.remove('../build/sconscript.log') + def test_bad_extension(self): - env = '' + env = {} with self.assertRaises(BadExtensionError): build_python('../build/py.py', ['bad', './input/python_test_script.py'], diff --git a/gslab_scons/tests/test_build_r.py b/gslab_scons/tests/test_build_r.py index 0ee9c88..604b4d0 100644 --- a/gslab_scons/tests/test_build_r.py +++ b/gslab_scons/tests/test_build_r.py @@ -21,16 +21,24 @@ def setUp(self): os.mkdir('../build/') def test_default(self): - env = '' + env = {} build_r('../build/r.rds', './input/R_test_script.R', env) logfile_data = open('../build/sconscript.log', 'rU').read() self.assertIn('Log created:', logfile_data) if os.path.isfile('../build/sconscript.log'): os.remove('../build/sconscript.log') + def test_clarg(self): + env = {'CL_ARG' : 'COMMANDLINE'} + build_r('../build/r.rds', './input/R_test_script.R', env) + logfile_data = open('../build/sconscript.log', 'rU').read() + self.assertIn('COMMANDLINE', logfile_data) + if os.path.isfile('../build/sconscript.log'): + os.remove('../build/sconscript.log') + def test_bad_extension(self): '''Test that build_r() recognises an inappropriate file extension''' - env = '' + env = {} with self.assertRaises(BadExtensionError): build_r('../build/r.rds', ['bad', './input/R_test_script.R'], env) diff --git a/gslab_scons/tests/test_build_stata.py b/gslab_scons/tests/test_build_stata.py index 4375f27..8bf4bf2 100644 --- a/gslab_scons/tests/test_build_stata.py +++ b/gslab_scons/tests/test_build_stata.py @@ -29,7 +29,15 @@ def test_default(self): self.assertIn('Log created:', logfile_data) if os.path.isfile('./build/sconscript.log'): os.remove('./build/sconscript.log') - + + def test_clarg(self): + env = {'user_flavor' : None, 'CL_ARG' : 'COMMANDLINE'} + build_stata('./build/stata.dta', './input/stata_test_script.do', env) + logfile_data = open('./build/sconscript.log', 'rU').read() + self.assertIn('COMMANDLINE', logfile_data) + if os.path.isfile('./build/sconscript.log'): + os.remove('./build/sconscript.log') + @unittest.skipIf(sys.platform.startswith("win"), "skipped test_user_executable_unix because on a windows machine") def test_user_executable_unix(self): diff --git a/gslab_scons/tests/test_misc.py b/gslab_scons/tests/test_misc.py index a6f0ee9..67e7cf8 100644 --- a/gslab_scons/tests/test_misc.py +++ b/gslab_scons/tests/test_misc.py @@ -22,18 +22,20 @@ def test_check_lfs(self): @unittest.skipIf(sys.platform.startswith("win"), "skipped test_stata_command_unix because on a windows machine") def test_stata_command_unix(self): - output = misc.stata_command_unix('flavor') + cl_arg = 'test' + output = misc.stata_command_unix('flavor', cl_arg) if sys.platform == 'darwin': option = '-e' else: option = '-b' - self.assertEqual(output, 'flavor %s' % (option) + ' %s ') + self.assertEqual(output, 'flavor %s' % (option) + ' %s ' + cl_arg) @unittest.skipUnless(sys.platform.startswith("win"), "skipped test_stata_command_win because on a unix machine") def test_stata_command_win(self): - output = misc.stata_command_win('flavor') - self.assertEqual(output, 'flavor %s' % ('/e do') + ' %s ') + cl_arg = 'test' + output = misc.stata_command_win('flavor', cl_arg) + self.assertEqual(output, 'flavor %s' % ('/e do') + ' %s ' + cl_arg) def test_is_unix(self): self.assertEqual(misc.is_unix(), not sys.platform.startswith("win")) @@ -41,6 +43,10 @@ def test_is_unix(self): def test_is_64_windows(self): pass + def test_command_line_arg(self): + self.assertEqual(misc.command_line_arg({'test' : ''}), '') + self.assertEqual(misc.command_line_arg({'CL_ARG' : 'Test'}), 'Test') + @unittest.skipIf(sys.platform.startswith("win"), "skipped test_is_in_path because on a windows machine") def test_is_in_path(self):