def test_run_cmd_ostr(): """ tbx.run(cmd, output='foobar') should raise error -- '>' or '|' required """ pytest.dbgfunc() with pytest.raises(tbx.Error) as err: tbx.run('python -c "import this"', output='foobar') assert '| or > required for string output' in str(err)
def test_run_noargs(): """ Without arguments, tbx.run() should throw a TypeError exception """ pytest.dbgfunc() with pytest.raises(TypeError) as err: tbx.run() assert 'run() takes' in str(err) assert 'argument' in str(err)
def test_run_cmd_ostr_redir(rdata, tmpdir): """ tbx.run(cmd, output='> foobar') should write data to foobar """ pytest.dbgfunc() outfile = tmpdir.join('outfile') target = '> ' + outfile.strpath tbx.run('python -c "import this"', output=target) result = outfile.read() for item in rdata.exp: assert item in result
def test_run_cmd_istr(rdata): """ tbx.run(cmd, input=str) """ pytest.dbgfunc() result = tbx.run('python', input='import this\n') for item in rdata.exp: assert item in result
def test_run_cmd_istrio(rdata): """ tbx.run(cmd, input=StringIO) """ pytest.dbgfunc() result = tbx.run('python', input=StringIO.StringIO('import this\n')) for item in rdata.exp: assert item in result
def test_run_cmd(rdata): """ With just a command (*cmd*), tbx.run() should run the command and return its stdout + stderr """ pytest.dbgfunc() result = tbx.run("python -c 'import this'") for item in rdata.exp: assert item in result
def test_run_cmd_icmd(rdata): """ tbx.run(cmd, input='cmd |') """ pytest.dbgfunc() icmd = "echo 'import this' |" result = tbx.run('python', input=icmd) for item in rdata.exp: assert item in result
def dq_run(cmd, dryrun, quiet): """ Handle a command subject to the dryrun and quiet members of options. !dryrun & !quiet: display cmd then run !dryrun & quiet: run without displaying first dryrun & !quiet: display cmd without running it dryrun & quiet: do nothing -- no display, no run """ if dryrun: print("would do '{}'".format(cmd)) elif quiet: result = tbx.run(cmd) print(result.rstrip()) else: print(cmd) result = tbx.run(cmd) print(result.rstrip())
def test_run_cmd_ipath(rdata, tmpdir): """ tbx.run(cmd, input='< path') """ pytest.dbgfunc() input_file = tmpdir.join('script') input_file.write('import this\n') result = tbx.run('python', input='< {0}'.format(input_file)) for item in rdata.exp: assert item in result
def test_now_nofmt_zone(): """ 'nldt -z local now' (with zone local, no format) should produce the current local time in default format (ISO). """ pytest.debug_func() exp = time.time() # payload result = tbx.run('nldt now -z local') repoch = time.mktime(time.strptime(result.strip(), "%Y-%m-%d %H:%M:%S")) assert abs(repoch - exp) < 1.0
def test_run_cmd_ifobj(rdata, tmpdir): """ tbx.run(cmd, input=open(filename, 'r')) """ pytest.dbgfunc() infile = tmpdir.join('infile') infile.write('import this\n') fobj = infile.open(mode='r') result = tbx.run('python', input=fobj) for item in rdata.exp: assert item in result
def test_run_cmd_ostrio(rdata): """ tbx.run(cmd, output=StringIO) output should wind up in the StringIO object """ pytest.dbgfunc() outstr = StringIO.StringIO() rval = tbx.run('python -c "import this"', output=outstr) assert rval is None result = outstr.getvalue() for item in rdata.exp: assert item in result
def test_next_weekday(weekday): """ 'nldt next monday' should generate the date for next Monday """ pytest.debug_func() # payload result = tbx.run("nldt next {}".format(weekday)) result = result.strip() # rm = nldt.moment(result) # exp = rm("%F %T", otz='local') exp = nl_oracle("next {}".format(weekday)) assert result == exp
def test_xargs_cmdl_stdin(tmpdir): """ Run 'ls /usr/include/nfs | fx -n -x -c "echo foo % bar"' Expected output: 'foo <file> <file> <file> ... <file> bar' """ pytest.dbgfunc() ilst = "one two three four".split() exp = "would do 'echo foo {} bar'\n".format(" ".join(ilst)) result = tbx.run("python fx xargs -n \"echo foo % bar\"", input="\n".join(ilst)) assert result == exp
def test_unanchored_noarg(): """ 'nldt' with no arguments should behave like 'nldt now' (like dt(1)). We do set the format (-f '...') but provide no date/time expression """ pytest.debug_func() # payload result = tbx.run('nldt -f "%s %F %T"') epoch, ymd = result.split(" ", 1) epoch = int(epoch) now = time.time() assert abs(int(now) - epoch) < 2 assert ymd.strip() == xtime(fmt="%F %T", when=epoch)
def test_unanchored_now(): """ 'nldt now' should give the current UTC time. By including %s in the output format, we can get the time index to verify against. """ pytest.debug_func() # payload result = tbx.run('nldt now -f "%s %F %T"') epoch, ymd = result.split(" ", 1) epoch = int(epoch) now = time.time() assert abs(int(now) - epoch) < 2 assert ymd.strip() == xtime(fmt="%F %T", when=epoch)
def test_cmdline(cmd, exp): """ Test nldt on the command line Note that the unanchored tests (i.e., those with no -w/--when on the nldt command) are liable to fail if the test suite is run within 15 seconds or so of the change of an hour. If this is a problem, reduce the resolution to the day level. This risk cannot be completely eliminated without anchoring the time reference, which would defeat the purpose of these tests. """ pytest.debug_func() result = tbx.run(cmd) assert result.strip() == exp
def test_run_cmd_ocmd(rdata): """ tbx.run(cmd1, '| cmd2') should pipe the output of cmd1 to cmd2 """ pytest.dbgfunc() cmd1 = "python -c 'import this'" cmd2 = "grep better" result = tbx.run(cmd1, output='| {0}'.format(cmd2)) for item in [_ for _ in rdata.exp if 'better' in _]: assert item in result for item in [_ for _ in rdata.exp if 'better' not in _]: assert item not in result
def test_run_cmd_ofobj(rdata, tmpdir): """ tbx.run(cmd, output=fileobj) should write output into fileobj """ pytest.dbgfunc() outfile = tmpdir.join('outfile') rval = tbx.run('python -c "import this"', output=open(outfile.strpath, 'w')) assert rval is None result = outfile.read() for item in rdata.exp: assert item in result
def test_run_cmd_opath(rdata, tmpdir): """ tbx.run(cmd, input={str, StringIO, '< path', '| cmd', fd, fileobj}, output={str, StringIO, '> path', 'cmd |', fd, fileobj}) """ pytest.dbgfunc() outfile = tmpdir.join('outfile') rval = tbx.run('python -c "import this"', output="> {0}".format(outfile.strpath)) assert rval is None result = outfile.read() for item in rdata.exp: assert item in result
def test_fx_short_help(): """ Verify that 'fx --help' does the right thing """ pytest.dbgfunc() exp_l = [ "Usage:", " fx [-d] [-n] [-q] cmd COMMAND FILE ...", " fx [-d] [-n] [-q] xargs COMMAND", " fx [-d] [-n] [-q] count COMMAND -i RANGE", " fx [-d] [-n] [-q] rename -e SUBSTITUTION FILE ...", " fx [-d] version", ] result = tbx.run("python fx help") assert result == "\n".join(exp_l) + "\n"
def test_fx_long_help(): """ Verify that 'fx --help' does the right thing """ pytest.dbgfunc() exp_l = [ "Usage:", " fx [-d] [-n] [-q] cmd COMMAND FILE ...", " fx [-d] [-n] [-q] xargs COMMAND", " fx [-d] [-n] [-q] count COMMAND -i RANGE", " fx [-d] [-n] [-q] rename -e SUBSTITUTION FILE ...", " fx [-d] version", "", "Options:", " -d debug -- run the python debugger", " -n dryrun -- just show what would happen", " -q quiet -- don't echo commands before running them", " -e SUBSTITUTION -- a substitute expression: " "s/foo/bar/", " -i RANGE <low number>:<high number>", ] result = tbx.run("python fx --help") assert result == "\n".join(exp_l) + "\n"
def _xcall(self, url): """ Run xcall and pass it a url """ result = tbx.run("{} -url \"{}\"".format(self._xcall_path(), url)) if result == '': return result while type(result) == str: result = json.loads(result) if 'errorMessage' in result: msg = result['errorMessage'] if 'tag' in msg: q = re.findall("tag=([^&]+)", url) if q: tag = q[0] msg = msg.replace('tag ', 'tag ' + tag + ' ') raise Bearror(msg) elif 'note' in result: pass elif '' in result.keys() and len(result) == 2: [other] = [_ for _ in result.keys() if _ != ''] result = json.loads(result[other]) return result
def test_zlint(): """ Run flake8 on the payload and test code """ result = tbx.run('flake8 tbx test') assert result == ''
def test_flake(): """ Vet the code quality """ result = tbx.run("flake8 fx test") assert result == ""
def git_commit(message=""): """ Run 'git commit' with *message* """ cmd = "git commit -m \"{}\"".format(message) tbx.run(cmd)
def git_push(): """ Run 'git push' """ tbx.run("git push")