def test_subst_rename_quiet(tmpdir, capsys): """ Test subst_rename() with dryrun False and quiet True. """ pytest.dbgfunc() with tbx.chdir(tmpdir.strpath): arglist = ['a.pl', 'b.pl', 'c.pl'] for a in arglist: tmpdir.join(a).ensure() expected = "".join([ 'rename %s.pl %s.xyzzy\n' % (x, x) for x in [q.replace('.pl', '') for q in arglist] ]) exp = [re.sub('.pl', '.xyzzy', x) for x in arglist] v = { '-n': False, '-q': True, '-e': True, 'SUBSTITUTION': "s/.pl/.xyzzy" } fx.subst_rename(v, arglist) assert expected in " ".join(capsys.readouterr()) q = glob.glob('*.pl') q.sort() assert [] == q q = glob.glob('*.xyzzy') q.sort() assert exp == q
def test_td_dhms(inp, exp): """ test td.dhhmmss() """ pytest.dbgfunc() obj = td(inp) assert obj.dhms() == exp
def test_chdir_rug(tmpdir): """ Trying to chdir out af a non existent directory (i.e., one that was removed after we chdir'd into it) """ pytest.dbgfunc() origin = os.getcwd() rug = tmpdir.join('foo/bar/nosuch') rug.ensure(dir=True) with tbx.chdir(rug.strpath): shutil.rmtree(rug.dirname) with pytest.raises(OSError) as err: with tbx.chdir('..'): assert os.getcwd() == origin assert any([ 'No such file or directory' in str(err.value), 'Invalid argument' in str(err.value) ]) with pytest.raises(OSError) as err: assert os.getcwd() == rug.strpath assert os.getcwd() == origin assert any([ 'No such file or directory' in str(err.value), 'Invalid argument' in str(err.value) ])
def test_function_doc(): """ Verify that each of our functions have a __doc__ string """ pytest.dbgfunc() importables = ['pyppi.__main__'] importables.extend([tbx.basename(_).replace('.py', '') for _ in glob.glob('tests/*.py')]) missing_doc = [] for mname in importables: mod = import_module(mname) for name, obj in inspect.getmembers(mod, inspect.isclass): if doc_missing(obj) and name not in missing_doc: missing_doc.append(name) for mthname, mthobj in inspect.getmembers(obj, inspect.isfunction): if name in ['pypath', 'local']: continue if doc_missing(mthobj) and mthname not in missing_doc: missing_doc.append("{}.{}".format(name, mthname)) for name, obj in inspect.getmembers(mod, inspect.isfunction): if doc_missing(obj) and name not in missing_doc: missing_doc.append("{}.{}".format(mname, name)) if missing_doc: pytest.fail("Items missing __doc__:\n " + "\n ".join(missing_doc))
def test_build_index_htmls(tmpdir, fx_cfgfile): """ After calling build_index_htmls(), verify that the expected files exist and have (approximately) the right content """ pytest.dbgfunc() cfg = fx_cfgfile root = pypath(cfg['root']) pkg_d = cfg['packages'] pmain.build_dirs(cfg) pmain.build_index_htmls(cfg) # payload rootidx = root.join("index.html") assert rootidx.exists() assert "<!DOCTYPE html>" in rootidx.read() base = root.basename for pkgname in pkg_d: assert "/{}/{}/".format(base, pkgname) in rootidx.read() pkgidx = root.join(pkgname, "index.html") assert "<!DOCTYPE html>" in pkgidx.read() for release in pkg_d[pkgname]: assert release['url'] in pkgidx.read() assert release['version'] in pkgidx.read() if 'minpy' in release: assert release['minpy'] in pkgidx.read()
def test_version(): """ Verify that tbx.version() returns a valid version string """ pytest.dbgfunc() assert re.match(r"\d\.\d\.\d", tbx.version()) assert tbx.version() == tbx.verinfo._v
def test_contents_good_str(ctest): """ Calling contents on a file that exists as a string """ pytest.dbgfunc() result = tbx.contents(ctest.data.strpath) assert result == ctest.exp
def test_nodoc(): """ Report routines missing a doc string """ pytest.dbgfunc() # get our bearings -- where is hpssic? hpssic_dir = U.dirname(sys.modules['hpssic'].__file__) excludes = ['setup.py', '__init__.py'] # up a level from there, do we have a '.git' directory? That is, are we in # a git repository? If so, we want to talk the whole repo for .py files hpssic_par = U.dirname(hpssic_dir) if not os.path.isdir(U.pathjoin(hpssic_par, ".git")): # otherwise, we just work from hpssic down wroot = hpssic_dir else: wroot = hpssic_par # collect all the .py files in pylist pylist = [] for r, dlist, flist in os.walk(wroot): if '.git' in dlist: dlist.remove('.git') pylist.extend([ U.pathjoin(r, x) for x in flist if x.endswith('.py') and x not in excludes ]) # make a list of the modules implied in pylist in mdict. Each module name # is a key. The associated value is False until the module is checked. mlist = ['hpssic'] for path in pylist: # Throw away the hpssic parent string, '.py' at the end, and split on # '/' to get a list of the module components mp = path.replace(hpssic_par + '/', '').replace('.py', '').split('/') if 1 < len(mp): fromlist = ['hpssic'] else: fromlist = [] mname = '.'.join(mp) if mname.startswith('hpssic'): mlist.append(mname) if mname not in sys.modules and mname.startswith('hpssic'): try: __import__(mname, fromlist=fromlist) except ImportError: pytest.fail('Failure trying to import %s' % mname) result = '' for m in mlist: result += nodoc_check(sys.modules[m], pylist, 0, 't') # result = nodoc_check(hpssic, 0, 't') if result != '': result = "The following functions need doc strings:\n" + result pytest.fail(result)
def test_open_note_title_succ(tnt): # noqa: F811 """ Test for bear.open_note() """ pytest.dbgfunc() by_title = tnt["cub"].open_note(title=tnt["title"]) assert by_title['title'] == tnt['title']
def test_contents_good_list(ctest): """ Calling contents on a file that exists as a list """ pytest.dbgfunc() result = tbx.contents(ctest.data.strpath, fmt='list') assert result == ctest.exp.split('\n')
def test_has_tag(tnt): # noqa: F811 """ Test for bear.has_tag(). Use fixture tnt. """ pytest.dbgfunc() assert tnt["cub"].has_tag(tnt["id"], "testing") assert not tnt["cub"].has_tag(tnt["id"], "newtag2")
def tzst_create_file_only(tmpdir): """ create a note with just a file (filename required). Creating a note with just a file and no title or text fails on the note being empty. After creating the note as here, there's no evidence of the file in the note. Aha! I was overwriting the file content with the file name. Let's try it again... Well, when I pass both file and filename, with the file content base64 encoded, what I get is 'File not in a valid base64 form'. It's not clear what the problem is. Guess I'll need to reach out to bear again. """ pytest.dbgfunc() cub = Bear() testfile = tmpdir.join("bear-test-data") # data = "".join(["This is a file for testing bear\n", # "It contains a couple of lines\n", # "and is going to be inserted into a bear note\n"]) data = "A" testfile.write(data) frob = base64.b64encode(data.encode()) note = cub.create(title=beartest_title(), file=frob, filename=testfile.strpath) if not note: pytest.fail("create returned empty result") cub.trash(id=note['identifier'])
def test_instantiate(): """ Make sure pytest is working """ pytest.dbgfunc() cub = Bear() assert 'open_note' in dir(cub)
def test_version(): """ Running 'fx version' should report the current fx version """ pytest.dbgfunc() result = tbx.run("python fx version") assert result == "fx {}\n".format(fx.version.__version__)
def test_task_proj(tasks): """ Given some tasks, report them and verify that they have whitespace between them """ pytest.dbgfunc() tmpdir = tasks['tmpdir'] kw = { 'PROJECT': 'myproj', 'count': False, 'd': False, 's': None, 'projects': False, 'tasks': True, 'version': False } with tbx.envset(GH_ROOT=tmpdir.strpath): result = ghm.gh_tasks_t(**kw) assert "\n\n - " in result kw = { 'PROJECT': 'nosuch', 'count': False, 'd': False, 's': None, 'projects': False, 'tasks': True, 'version': False } with tbx.envset(GH_ROOT=tmpdir.strpath): result = ghm.gh_tasks_t(**kw) assert result == ""
def test_xw_nopct_file(fx_batch): """ Tests for xargs_wrap(cmd: str, rble: fileobj) - cmd may contain '%', or not - fileobj may be an open file, sys.stdin, a StringIO What xargs_wrap is supposed to do, is, given a *cmd* like 'foo % bar', it takes lexical items from *rble* and constructs strings of the form 'foo item1 item2 item3 ... itemn bar' such that the strings are only minimally longer than 240 bytes. This is like xargs except that I've never figured out a way to get xargs to embed stuff in the middle of the command -- it only wants to put stuff at the end. If *cmd* does not contain a % character (or if all the % chars are escaped), xargs_wrap should put the items at the end of cmd. """ pytest.dbgfunc() tmppath, data = fx_batch with open(tmppath, 'r') as fobj: result = fx.xargs_wrap("echo", fobj) assert result == data
def test_sorts(prjdirs, func, sname): """ Test the sort routines """ pytest.dbgfunc() result = func(prjdirs['input']) # payload assert result == prjdirs[sname]
def test_delta(left, right, exp): """ Tests for _delta """ pytest.dbgfunc() q = dt() assert q._delta(left, right) == exp
def test_nodoc(): """ Report routines missing a doc string """ pytest.dbgfunc() # get our bearings -- where is hpssic? hpssic_dir = U.dirname(sys.modules['hpssic'].__file__) excludes = ['setup.py', '__init__.py'] # up a level from there, do we have a '.git' directory? That is, are we in # a git repository? If so, we want to talk the whole repo for .py files hpssic_par = U.dirname(hpssic_dir) if not os.path.isdir(U.pathjoin(hpssic_par, ".git")): # otherwise, we just work from hpssic down wroot = hpssic_dir else: wroot = hpssic_par # collect all the .py files in pylist pylist = [] for r, dlist, flist in os.walk(wroot): if '.git' in dlist: dlist.remove('.git') pylist.extend([U.pathjoin(r, x) for x in flist if x.endswith('.py') and x not in excludes]) # make a list of the modules implied in pylist in mdict. Each module name # is a key. The associated value is False until the module is checked. mlist = ['hpssic'] for path in pylist: # Throw away the hpssic parent string, '.py' at the end, and split on # '/' to get a list of the module components mp = path.replace(hpssic_par + '/', '').replace('.py', '').split('/') if 1 < len(mp): fromlist = ['hpssic'] else: fromlist = [] mname = '.'.join(mp) if mname.startswith('hpssic'): mlist.append(mname) if mname not in sys.modules and mname.startswith('hpssic'): try: __import__(mname, fromlist=fromlist) except ImportError: pytest.fail('Failure trying to import %s' % mname) result = '' for m in mlist: result += nodoc_check(sys.modules[m], pylist, 0, 't') # result = nodoc_check(hpssic, 0, 't') if result != '': result = "The following functions need doc strings:\n" + result pytest.fail(result)
def test_contents_invalid_fmt(ctest): """ Calling contents with an invalid format """ pytest.dbgfunc() with pytest.raises(tbx.Error) as err: _ = tbx.contents(ctest.data.strpath, fmt='foobar', sep=r'\s') assert "Invalid format" in str(err.value)
def test_contents_good_altsep(ctest): """ Calling contents on a file that exists as a list """ pytest.dbgfunc() result = tbx.contents(ctest.data.strpath, fmt='list', sep=r'\s') assert len(result) == len(re.split(r'\s', ctest.exp)) assert result == re.split(r'\s', ctest.exp)
def test_grep(rgx, idx, grep_prep): """ Check the results of calling U.grep() """ pytest.dbgfunc() f = test_grep for n, s in enumerate(f.searches): assert U.grep(s, f.testdata, regex=rgx, index=idx) == f.exp[n]
def test_contents_list(tmpdir, contents_setup): """ Test getting the contents of a file """ pytest.dbgfunc() pt = pytest.this c = tbx.contents(pt['tfn'].strpath, type=list) assert c == [x + '\n' for x in pt['td']]
def test_run_stdout(): """ Run a process and capture stdout """ pytest.dbgfunc() r = tbx.run('python -c "import this"') assert "Zen of Python" in r assert "Namespaces" in r
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_fail(): """ Run a binary that doesn't exist """ pytest.dbgfunc() r = tbx.run("nosuchbinary") assert "ERR:" in r assert "No such file or directory" in r
def test_no_sort(prjdirs): """ What we get if we don't sort a list of projects """ pytest.dbgfunc() tup_l = ghm.projects(prjdirs['root'].strpath, False) # payload result = [_[0] for _ in tup_l] assert set(result) == set([_.strpath for _ in prjdirs['input']])
def test_dodo_filename_x(dodo_existing): """ Run dodo_filename for a file that exists """ pytest.dbgfunc() (prjdir, dofile) = dodo_existing rval = ghm.dodo_filename(prjdir.strpath) # payload assert rval == dofile.strpath
def test_get_tasks_empty(tasks_empty): """ Make get_tasks() return an empty list because no tasks are present """ pytest.dbgfunc() path = tasks_empty['prj'].strpath task_l = ghm.get_tasks(path) assert task_l == []
def test_task_markers(tasks, capsys): """ Use all potential task markers to identify tasks """ pytest.dbgfunc() path = tasks['prj'].strpath task_l = ghm.get_tasks(path) assert len(task_l) == 4
def test_run_stderr(): """ Run a process and capture stdout """ pytest.dbgfunc() r = tbx.run("ls --nosuch") assert "ERR:" in r assert re.findall("(illegal|unrecognized) option", r)
def test_contents_badfmt(ctest): """ Calling contents on a file that exists as a list """ pytest.dbgfunc() with pytest.raises(tbx.Error) as err: _ = tbx.contents(ctest.data.strpath, fmt='str', sep=r'\s') assert 'Non-default separator is only valid for list format' in str(err)
def test_zlint(): """ Run flake8 on the payload and test code """ pytest.dbgfunc() result = subp.Popen(shlex.split("flake8 tbx test"), stdout=subp.PIPE).communicate()[0] assert result.decode() == ''
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_contents_str(tmpdir): """ Test getting the contents of a file """ pytest.dbgfunc() pt = pytest.this c = tbx.contents(pt['tfn'].strpath) assert c == ''.join([x + '\n' for x in pt['td']])
def test_contents_default(tmpdir): """ Test getting the contents of a file """ pytest.dbgfunc() pt = pytest.this c = tbx.contents(pt['tfn'].strpath + "_nonesuch", default="take this instead") assert "take this instead" in c
def test_chdir(tmpdir): """ Test the chdir contextmanager """ pytest.dbgfunc() here = os.getcwd() with tbx.chdir(tmpdir.strpath): assert os.getcwd() == tmpdir.strpath assert os.getcwd() == here
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_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_dispatch_help_nosuch(): """ Dispatch help on a function that does not exist """ pytest.dbgfunc() exp = 'Module test.test_tbx has no attribute foo_nosuch' with pytest.raises(SystemExit) as err: tbx.dispatch(__name__, 'foo', ['help', 'nosuch']) assert exp in str(err)
def test_dispatch_help_nopfx(): """ Dispatch help with no prefix """ pytest.dbgfunc() exp = '*prefix* is required' with pytest.raises(SystemExit) as err: tbx.dispatch(__name__, args=['help', 'nosuch']) assert exp in str(err)
def test_dispatch_help_nomodule(): """ Dispatch help on a non-existent module """ pytest.dbgfunc() exp = 'Module xtest.test_tbx is not in sys.modules' with pytest.raises(SystemExit) as err: tbx.dispatch('x' + __name__, 'foo', ['help', 'nosuch']) assert exp in str(err)
def test_dispatch_help_nodoc(): """ Dispatch help on a function that has no doc string """ pytest.dbgfunc() exp = 'Function xtst_undocumented is missing a __doc__ string' with pytest.raises(SystemExit) as err: tbx.dispatch_help(__name__, 'xtst', ['undocumented']) assert exp in str(err)
def test_dispatch_help_good(capsys): """ Dispatch help on a function that exists and has a doc string """ pytest.dbgfunc() exp = 'foobar - print a comma delimited argument list' tbx.dispatch_help(__name__, 'dtst', ['foobar']) out, _ = capsys.readouterr() assert exp in out
def contents_setup(tmpdir): """ set up data for test """ pytest.dbgfunc() pytest.this = {} td = pytest.this['td'] = ["one", "two", "three", "four", "five"] tfn = pytest.this['tfn'] = tmpdir.join("testthis") tfn.write(''.join([x+"\n" for x in td]))
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 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_contents_badperm(ctest): """ Calling contents on a file that is not readable with throw an exception """ pytest.dbgfunc() ctest.data.chmod(0000) with pytest.raises(tbx.Error) as err: _ = tbx.contents(ctest.data.strpath, fmt=str, sep=r'\s') assert "Can't read file {0}".format(ctest.data.strpath) in str(err)
def test_gitr_unimpl(subc, capsys): """ gitr <subc> -> 'coming soon' """ pytest.dbgfunc() func = getattr(gitr, 'gitr_' + subc) func({subc: True}) o, e = capsys.readouterr() assert 'Coming soon' in o
def test_contents_fail(tmpdir): """ Test getting the contents of a file """ pytest.dbgfunc() pt = pytest.this with pytest.raises(IOError) as e: c = tbx.contents(pt['tfn'].strpath + "_nonesuch") assert "No such file or directory" in str(e)
def test_docopt(argd, capsys): """ Test calls to docopt.docopt() that should be successful """ pytest.dbgfunc() exp = docopt_exp(**argd) argl = docopt_cmdl(argd) r = docopt.docopt(gitr.__doc__, argl) assert r == exp
def test_contents_nosuch_default(ctest): """ Attempting to get the contents of a non-existent file with a default value should return the default """ pytest.dbgfunc() filename = ctest.data.strpath + 'xxx' result = tbx.contents(filename, default='foobar') assert result == 'foobar'