From 91b5789f71a625bb35df8cbbe2d2ad5f4085894f Mon Sep 17 00:00:00 2001 From: Trevor Campbell Date: Thu, 31 Aug 2023 12:47:01 -0700 Subject: [PATCH] trying to figure out workdir test --- nbgrader/preprocessors/instantiatetests.py | 10 ++- .../preprocessors/test_instantiatetests.py | 65 +++++++++---------- 2 files changed, 40 insertions(+), 35 deletions(-) diff --git a/nbgrader/preprocessors/instantiatetests.py b/nbgrader/preprocessors/instantiatetests.py index 6e85e1fdd..5578df4a0 100644 --- a/nbgrader/preprocessors/instantiatetests.py +++ b/nbgrader/preprocessors/instantiatetests.py @@ -133,10 +133,16 @@ def preprocess(self, nb, resources): self.log.debug('Starting client for kernel %s at path %s', kernel_name, resources_path if resources_path is not None else '') km, self.kc = start_new_kernel(kernel_name=kernel_name, cwd=resources_path) + self.log.error('here is the notebook before') + self.log.error(nb) + # run the preprocessor self.log.debug('Running InstantiateTests preprocessor') nb, resources = super(InstantiateTests, self).preprocess(nb, resources) + self.log.error('here is the notebook after') + self.log.error(nb) + # shut down and cleanup the kernel self.log.debug('Shutting down / cleaning up kernel') km.shutdown_kernel() @@ -161,11 +167,11 @@ def preprocess_cell(self, cell, resources, index): # get the comment string for this language comment_str = self.comment_strs[resources["kernel_name"]] - # seed the salt generator for this cell + # seed the salt generator for this cell # avoid actual random seeds so that release versions are consistent across # calls to nbgrader generate_assignment salt_int = int(hashlib.sha256((cell.source+str(index)).encode('utf-8')).hexdigest(), 16) % 10**6 - + # split the code lines into separate strings lines = cell.source.split("\n") diff --git a/nbgrader/tests/preprocessors/test_instantiatetests.py b/nbgrader/tests/preprocessors/test_instantiatetests.py index cbba850f0..ff34ec69d 100644 --- a/nbgrader/tests/preprocessors/test_instantiatetests.py +++ b/nbgrader/tests/preprocessors/test_instantiatetests.py @@ -87,40 +87,40 @@ def test_consistent_release_version(self, preprocessor): nb2, resources2 = preprocessor.preprocess(nb2, resources2) assert nb1['cells'][1]['source'] == nb2['cells'][1]['source'] - ## test that autotest starts a kernel that uses the `path` metadata as working directory - #def test_kernel_workingdir(self, preprocessor, caplog): - # sol_cell = create_autotest_solution_cell() - # test_cell = create_autotest_test_cell() - # load_cell = create_file_loader_cell('grades.csv') - # test_cell.metadata['nbgrader'] = {'grade': True} + # test that autotest starts a kernel that uses the `path` metadata as working directory + def test_kernel_workingdir(self, preprocessor, caplog): + sol_cell = create_autotest_solution_cell() + test_cell = create_autotest_test_cell() + load_cell = create_file_loader_cell('grades.csv') + test_cell.metadata['nbgrader'] = {'grade': True} - # # with the right path, the kernel should load the file - # nb = new_notebook() - # nb.metadata['kernelspec'] = { - # "name": "python3" - # } - # nb.cells.append(sol_cell) - # nb.cells.append(test_cell) - # nb.cells.append(load_cell) - # resources = { - # 'metadata': {'path': 'nbgrader/docs/source/user_guide/'} - # } - # nb, resources = preprocessor.preprocess(nb, resources) + # with the right path, the kernel should load the file + nb = new_notebook() + nb.metadata['kernelspec'] = { + "name": "python3" + } + nb.cells.append(sol_cell) + nb.cells.append(test_cell) + nb.cells.append(load_cell) + resources = { + 'metadata': {'path': 'nbgrader/docs/source/user_guide/'} + } + nb, resources = preprocessor.preprocess(nb, resources) - # # without the right path, the kernel should report an error - # nb = new_notebook() - # nb.metadata['kernelspec'] = { - # "name": "python3" - # } - # nb.cells.append(sol_cell) - # nb.cells.append(test_cell) - # nb.cells.append(load_cell) - # resources = { - # 'metadata': {'path': 'nbgrader/docs/source/user_guide/source/'} - # } - # with pytest.raises(Exception): - # nb, resources = preprocessor.preprocess(nb, resources) - # assert "FileNotFoundError" in caplog.text + # without the right path, the kernel should report an error + nb = new_notebook() + nb.metadata['kernelspec'] = { + "name": "python3" + } + nb.cells.append(sol_cell) + nb.cells.append(test_cell) + nb.cells.append(load_cell) + resources = { + 'metadata': {'path': 'nbgrader/docs/source/user_guide/source/'} + } + with pytest.raises(Exception): + nb, resources = preprocessor.preprocess(nb, resources) + assert "FileNotFoundError" in caplog.text # test that a warning is thrown when we set enforce_metadata = False and have an AUTOTEST directive in a # non-grade cell @@ -159,7 +159,6 @@ def test_error_autotest_nongrade(self, preprocessor, caplog): with pytest.raises(Exception): nb, resources = preprocessor.preprocess(nb, resources) - assert False, caplog.text assert "AutoTest region detected in a non-grade cell; " in caplog.text # test that invalid python statements in AUTOTEST directives cause errors