def bootstrap_builders(): g = make_db() repo = next(g) os.chdir(repo) for bm in builder_map: if bm == "html": os.makedirs("templates/static") if bm == "figure": prep_figure() main(["build", bm, "--no-pdf"]) os.chdir(os.path.join(repo, "_build", bm)) os.makedirs(expected_base, exist_ok=True) for root, dirs, files in os.walk("."): for file in files: # Use this for bootstrapping the tests, # confirm by hand that files look correct if root == ".": os.makedirs(os.path.join(expected_base, bm), exist_ok=True) shutil.copyfile( os.path.join(file), os.path.join(expected_base, bm, file), ) else: os.makedirs( os.path.join(expected_base, bm, root), exist_ok=True ) shutil.copyfile( os.path.join(root, file), os.path.join(expected_base, bm, root, file), ) os.chdir(repo) next(g)
def test_helper_python_loose(hm, make_db, capsys): repo = Path(make_db) testfile = Path(__file__) os.chdir(repo) main(args=hm[0]) out, err = capsys.readouterr() assert hm[1] in out
def test_validate_python_single_col(make_db): repo = make_db os.chdir(repo) backup = sys.stdout sys.stdout = StringIO() main(["validate", "--collection", "people"]) out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup assert "NO ERRORS IN DBS" in out
def test_validate_python(make_db): repo = make_db os.chdir(repo) backup = sys.stdout sys.stdout = StringIO() main(["validate"]) out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup assert "NO ERRORS IN DBS" in out
def test_validate_bad_python(make_bad_db): repo = make_bad_db os.chdir(repo) backup = sys.stdout sys.stdout = StringIO() with pytest.raises(SystemExit): main(["validate"]) out = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup assert "Errors found in " in out assert "NO ERRORS IN DBS" not in out
def test_helper_python(hm, make_db, capsys): repo = Path(make_db) testfile = Path(__file__) os.chdir(repo) main(args=hm[0]) out, err = capsys.readouterr() assert out == hm[1] builddir = repo / "_build" / hm[0][1] expecteddir = testfile.parent / "outputs" / hm[0][1] are_outfiles = any(builddir.iterdir()) if are_outfiles and not expecteddir.is_dir(): print("WARNING: there are built outputs that are not being tested") if are_outfiles and expecteddir.is_dir(): assert_outputs(builddir, expecteddir) builddir = repo / "db" if expecteddir.is_dir(): assert_outputs(builddir, expecteddir)
def test_mongo_to_fs_python(make_mongo_to_fs_backup_db): repo = make_mongo_to_fs_backup_db os.chdir(repo) try: main(['mongo-to-fs']) except Exception as e: print(e) assert True == False else: assert True == True replace_rc_dbs(repo) os.chdir(repo) rc = copy.copy(DEFAULT_RC) rc._update(load_rcfile("regolithrc.json")) with connect(rc) as rc.client: fs_db = rc.client[FS_DB_NAME] mongo_db = rc.client[ALTERNATE_REGOLITH_MONGODB_NAME] for coll in mongo_db.list_collection_names(): migrated_fs_collection = fs_db[coll] original_mongo_collection = load_mongo_col(mongo_db[coll]) assert migrated_fs_collection == original_mongo_collection
def test_builder_python(bm, make_db): repo = make_db os.chdir(repo) if bm == "figure": prep_figure() if bm == "html": os.makedirs("templates/static", exist_ok=True) if bm == "reimb": main(["build", bm, "--no-pdf", "--people", "scopatz"]) else: main(["build", bm, "--no-pdf"]) os.chdir(os.path.join(repo, "_build", bm)) expected_base = os.path.join(os.path.dirname(__file__), "outputs") for root, dirs, files in os.walk("."): for file in files: if file in os.listdir(os.path.join(expected_base, bm, root)): fn1 = os.path.join(repo, "_build", bm, root, file) if bm == "reimb": actual = openpyxl.load_workbook(fn1)["T&B"] actual = [str(actual[b]) for b in xls_check] else: with open(fn1, "r") as f: actual = f.read() fn2 = os.path.join(expected_base, bm, root, file) if bm == "reimb": expected = openpyxl.load_workbook(fn2)["T&B"] expected = [str(expected[b]) for b in xls_check] else: with open(fn2, "r") as f: expected = f.read() # Skip because of a date time in if file != "rss.xml": # Fixme proper fix for testing hard coded filepaths on windows if os.name == "nt": if "tmp" not in expected: if "../.." not in expected: assert expected == actual else: assert expected == actual
def test_user_rc(make_db): repo = make_db DEFAULT_RC.user_config = os.path.join(repo, "user.json") os.chdir(repo) backup = sys.stdout sys.stdout = StringIO() main(["rc"]) out1 = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup with open(DEFAULT_RC.user_config, "w") as f: json.dump({"hello": "world"}, f) backup = sys.stdout sys.stdout = StringIO() main(["rc"]) out2 = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup assert out1 != out2 assert "hello" in out2
def test_user_rc(make_db): repo = make_db DEFAULT_RC.user_config = os.path.join(repo, 'user.json') os.chdir(repo) backup = sys.stdout sys.stdout = StringIO() main(['rc']) out1 = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup with open(DEFAULT_RC.user_config, 'w') as f: json.dump({'hello': 'world'}, f) backup = sys.stdout sys.stdout = StringIO() main(['rc']) out2 = sys.stdout.getvalue() sys.stdout.close() sys.stdout = backup assert out1 != out2 assert 'hello' in out2
def test_builder_python(bm, make_db): repo = make_db os.chdir(repo) if bm == 'html': os.makedirs('templates/static', exist_ok=True) main(['build', bm, '--no-pdf']) os.chdir(os.path.join(repo, '_build', bm)) expected_base = os.path.join(os.path.dirname(__file__), 'outputs') for root, dirs, files in os.walk('.'): for file in files: if file in os.listdir(os.path.join(expected_base, bm, root)): with open(os.path.join(repo, '_build', bm, root, file), 'r') as f: actual = f.read() with open(os.path.join(expected_base, bm, root, file), 'r') as f: expected = f.read() # Skip because of a date time in if file != 'rss.xml': assert expected == actual
def test_builder_python(bm, make_db): repo = make_db os.chdir(repo) if bm == "figure": prep_figure() if bm == "html": os.makedirs("templates/static", exist_ok=True) if bm == "reimb" or bm == "recent-collabs": main(["build", bm, "--no-pdf", "--people", "scopatz"]) elif bm == "annual-activity": main([ "build", bm, "--no-pdf", "--people", "sbillinge", "--from", "2017-04-01" ]) else: main(["build", bm, "--no-pdf"]) os.chdir(os.path.join(repo, "_build", bm)) expected_base = os.path.join(os.path.dirname(__file__), "outputs") for root, dirs, files in os.walk("."): for file in files: if file in os.listdir(os.path.join(expected_base, bm, root)): fn1 = os.path.join(repo, "_build", bm, root, file) if bm == "reimb": actual = openpyxl.load_workbook(fn1)["T&B"] actual = [str(actual[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn1: sheet = "NSF COA Template" else: sheet = "Collaborator Template" actual = openpyxl.load_workbook(fn1)[sheet] actual = [ str(actual[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn1, "r") as f: actual = f.read() fn2 = os.path.join(expected_base, bm, root, file) if bm == "reimb": expected = openpyxl.load_workbook(fn2)["T&B"] expected = [str(expected[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn2: sheet = "NSF COA Template" else: sheet = "Collaborator Template" expected = openpyxl.load_workbook(fn2)[sheet] expected = [ str(expected[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn2, "r") as f: expected = f.read() # Skip because of a date time in if file != "rss.xml": if file.endswith('.html') or file.endswith('.tex'): assert is_same(expected, actual, ['../..', 'tmp']) else: assert expected == actual
def test_builder_python(bm, db_src, make_db, make_mongodb, monkeypatch): if db_src == "fs": repo = make_db elif db_src == "mongo": if make_mongodb is False: pytest.skip("Mongoclient failed to start") else: repo = make_mongodb os.chdir(repo) if bm == "figure": prep_figure() if bm == "internalhtml": def mockreturn(*args, **kwargs): mock_article = { 'message': { 'author': [{ "given": "SJL", "family": "B" }], "short-container-title": ["J Club Paper"], "volume": 10, "title": ["title"], "issued": { "date-parts": [[1971]] } } } return mock_article monkeypatch.setattr(habanero.Crossref, "works", mockreturn) if bm == "html": os.makedirs("templates/static", exist_ok=True) if bm == "reimb" or bm == "recent-collabs": main(["build", bm, "--no-pdf", "--people", "scopatz"]) elif bm == "annual-activity": main([ "build", bm, "--no-pdf", "--people", "sbillinge", "--from", "2017-04-01" ]) else: main(["build", bm, "--no-pdf"]) os.chdir(os.path.join(repo, "_build", bm)) expected_base = os.path.join(os.path.dirname(__file__), "outputs") for root, dirs, files in os.walk("."): for file in files: if file in os.listdir(os.path.join(expected_base, bm, root)): fn1 = os.path.join(repo, "_build", bm, root, file) if bm == "reimb": actual = openpyxl.load_workbook(fn1)["T&B"] actual = [str(actual[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn1: sheet = "NSF COA Template" else: sheet = "Collaborator Template" actual = openpyxl.load_workbook(fn1)[sheet] actual = [ str(actual[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn1, "r") as f: actual = f.read() fn2 = os.path.join(expected_base, bm, root, file) if bm == "reimb": expected = openpyxl.load_workbook(fn2)["T&B"] expected = [str(expected[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn2: sheet = "NSF COA Template" else: sheet = "Collaborator Template" expected = openpyxl.load_workbook(fn2)[sheet] expected = [ str(expected[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn2, "r") as f: expected = f.read() # Skip because of a date time in if file != "rss.xml": if file.endswith('.html') or file.endswith('.tex'): if not is_same(expected, actual, ['../..', 'tmp']): assert actual == expected else: assert actual == expected
def test_version(): sys.stdout = StringIO() main(["--version"]) assert sys.stdout.getvalue() == "{}\n".format(__version__)
def test_builder(bm, db_src, make_db, make_mongodb, monkeypatch): # FIXME: Somehow the mongo backend failed to build figure # FIXME: now fs is failing to build figure # if db_src == "mongo" and bm == "figure": if bm == "figure": return if db_src == "fs": repo = make_db elif db_src == "mongo": if make_mongodb is False: pytest.skip("Mongoclient failed to start") else: repo = make_mongodb else: raise ValueError("Unknown database source: {}".format(db_src)) os.chdir(repo) if bm == "figure": prep_figure() if bm == "internalhtml": # for some reason the mocking of the crossref call doesn't work when the # test is run using subprocess, so skip in this case. # the functionality is fully tested in test_builder_python pytest.skip("mocking of Crossref not working with subprocess") if bm == "html": os.makedirs("templates/static", exist_ok=True) if bm == "reimb" or bm == "recent-collabs": subprocess.run( ["regolith", "build", bm, "--no-pdf", "--people", "scopatz"], check=True, cwd=repo) elif bm == "annual-activity": subprocess.run([ "regolith", "build", bm, "--no-pdf", "--people", "sbillinge", "--from", "2017-04-01" ], check=True, cwd=repo) elif bm == "grantreport": main([ "build", bm, "--no-pdf", "--grant", "SymPy-1.1", "--from", "2017-04-01", "--to", "2018-03-31" ]) else: subprocess.run(["regolith", "build", bm, "--no-pdf"], check=True, cwd=repo) os.chdir(os.path.join(repo, "_build", bm)) expected_base = os.path.join(os.path.dirname(__file__), "outputs") for root, dirs, files in os.walk("."): for file in files: if file in os.listdir(os.path.join(expected_base, bm, root)): fn1 = os.path.join(repo, "_build", bm, root, file) if bm == "reimb": actual = openpyxl.load_workbook(fn1)["T&B"] actual = [str(actual[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn1: sheet = "NSF COA Template" else: sheet = "Collaborators" actual = openpyxl.load_workbook(fn1)[sheet] actual = [ str(actual[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn1, "r") as f: actual = f.read() fn2 = os.path.join(expected_base, bm, root, file) if bm == "reimb": expected = openpyxl.load_workbook(fn2)["T&B"] expected = [str(expected[b]) for b in xls_check] elif bm == "recent-collabs": if 'nsf' in fn2: sheet = "NSF COA Template" else: sheet = "Collaborators" expected = openpyxl.load_workbook(fn2)[sheet] expected = [ str(expected[cell]) for cell in recent_collabs_xlsx_check ] else: with open(fn2, "r") as f: expected = f.read() # Skip because of a date time in if file != "rss.xml": if file.endswith('.html') or file.endswith('.tex'): if not is_same(expected, actual, ['../..', 'tmp']): assert actual == expected else: assert actual == expected