def test_conda_env_export(self): fake_conda = join(dirname(__file__), "testdata", "fake_conda.sh") result = detect_environment(get_dir("conda1"), conda_mode=True, force_generate=True, conda=fake_conda) self.assertEqual(result.source, "conda_env_export") self.assertEqual(result.conda, "1.0.0") self.assertEqual(result.contents, "this is a conda environment\n") fake_broken_conda = join(dirname(__file__), "testdata", "fake_broken_conda.sh") self.assertRaises( EnvironmentException, detect_environment, get_dir("conda1"), conda_mode=True, force_generate=True, conda=fake_broken_conda, )
def test_file(self): result = detect_environment(get_dir("pip1")) self.assertTrue(version_re.match(result.pip)) self.assertIsInstance(result.locale, str) self.assertIn(".", result.locale) expected = Environment( contents="numpy\npandas\nmatplotlib\n", filename="requirements.txt", locale=result.locale, package_manager="pip", pip=result.pip, python=self.python_version(), source="file", ) self.assertEqual(expected, result)
def test_pip_freeze(self): result = detect_environment(get_dir("pip2")) # these are the dependencies declared in our setup.py self.assertIn("six", result.contents) self.assertIn("click", result.contents.lower()) self.assertTrue(version_re.match(result.pip)) self.assertIsInstance(result.locale, str) self.assertIn(".", result.locale) expected = Environment( contents=result.contents, filename="requirements.txt", locale=result.locale, package_manager="pip", pip=result.pip, python=self.python_version(), source="pip_freeze", ) self.assertEqual(expected, result)
def test_source_bundle2(self): self.maxDiff = 5000 directory = get_dir("pip2") nb_path = join(directory, "dummy.ipynb") # Note that here we are introspecting the environment from within # the test environment. Don't do this in the production code, which # runs in the notebook server. We need the introspection to run in # the kernel environment and not the notebook server environment. environment = detect_environment(directory) with make_notebook_source_bundle( nb_path, environment, extra_files=["data.csv" ]) as bundle, tarfile.open(mode="r:gz", fileobj=bundle) as tar: names = sorted(tar.getnames()) self.assertEqual(names, [ "data.csv", "dummy.ipynb", "manifest.json", "requirements.txt", ]) reqs = tar.extractfile("requirements.txt").read() # these are the dependencies declared in our setup.py self.assertIn(b"six", reqs) manifest = json.loads( tar.extractfile("manifest.json").read().decode("utf-8")) # don't check requirements.txt since we don't know the checksum del manifest["files"]["requirements.txt"] # also don't check locale value, just require it be present del manifest["locale"] del manifest["python"]["package_manager"]["version"] if sys.version_info[0] == 2: ipynb_hash = u"38aa30662bc16e91e6804cf21d7722f7" else: ipynb_hash = u"36873800b48ca5ab54760d60ba06703a" # noinspection SpellCheckingInspection self.assertEqual( manifest, { u"version": 1, u"metadata": { u"appmode": u"jupyter-static", u"entrypoint": u"dummy.ipynb", }, u"python": { u"version": self.python_version(), u"package_manager": { u"name": u"pip", u"package_file": u"requirements.txt", }, }, u"files": { u"dummy.ipynb": { u"checksum": ipynb_hash, }, u"data.csv": { u"checksum": u"f2bd77cc2752b3efbb732b761d2aa3c3" }, }, }, )
def test_source_bundle1(self): self.maxDiff = 5000 directory = get_dir("pip1") nb_path = join(directory, "dummy.ipynb") # Note that here we are introspecting the environment from within # the test environment. Don't do this in the production code, which # runs in the notebook server. We need the introspection to run in # the kernel environment and not the notebook server environment. environment = detect_environment(directory) with make_notebook_source_bundle( nb_path, environment) as bundle, tarfile.open(mode="r:gz", fileobj=bundle) as tar: names = sorted(tar.getnames()) self.assertEqual(names, [ "dummy.ipynb", "manifest.json", "requirements.txt", ]) reqs = tar.extractfile("requirements.txt").read() self.assertEqual(reqs, b"numpy\npandas\nmatplotlib\n") manifest = json.loads( tar.extractfile("manifest.json").read().decode("utf-8")) # don't check locale value, just require it be present del manifest["locale"] del manifest["python"]["package_manager"]["version"] if sys.version_info[0] == 2: ipynb_hash = u"38aa30662bc16e91e6804cf21d7722f7" else: ipynb_hash = u"36873800b48ca5ab54760d60ba06703a" # noinspection SpellCheckingInspection self.assertEqual( manifest, { u"version": 1, u"metadata": { u"appmode": u"jupyter-static", u"entrypoint": u"dummy.ipynb", }, u"python": { u"version": self.python_version(), u"package_manager": { u"name": u"pip", u"package_file": u"requirements.txt", }, }, u"files": { u"dummy.ipynb": { u"checksum": ipynb_hash, }, u"requirements.txt": { u"checksum": u"5f2a5e862fe7afe3def4a57bb5cfb214" }, }, }, )