Skip to content

Commit

Permalink
trying to figure out workdir test
Browse files Browse the repository at this point in the history
  • Loading branch information
trevorcampbell committed Aug 31, 2023
1 parent 037141d commit 91b5789
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 35 deletions.
10 changes: 8 additions & 2 deletions nbgrader/preprocessors/instantiatetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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")

Expand Down
65 changes: 32 additions & 33 deletions nbgrader/tests/preprocessors/test_instantiatetests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 91b5789

Please sign in to comment.