From 537da288f33711831d44fe8d181f31094a96f226 Mon Sep 17 00:00:00 2001 From: Ahmet Altay Date: Wed, 25 Jan 2017 09:57:18 -0800 Subject: [PATCH] Use a temp directory for requirements cache in test_with_requirements_file The test fails if there are leftover files in the default folder for requirements cache either from earlier tests, or from the previous workspaces. --- .../apache_beam/utils/dependency_test.py | 47 +++++++++++-------- 1 file changed, 27 insertions(+), 20 deletions(-) diff --git a/sdks/python/apache_beam/utils/dependency_test.py b/sdks/python/apache_beam/utils/dependency_test.py index a484d60157d8d..75a89e28ef813 100644 --- a/sdks/python/apache_beam/utils/dependency_test.py +++ b/sdks/python/apache_beam/utils/dependency_test.py @@ -106,27 +106,34 @@ def test_default_resources(self): dependency.stage_job_resources(options)) def test_with_requirements_file(self): - staging_dir = tempfile.mkdtemp() - source_dir = tempfile.mkdtemp() + try: + staging_dir = tempfile.mkdtemp() + requirements_cache_dir = tempfile.mkdtemp() + source_dir = tempfile.mkdtemp() - options = PipelineOptions() - options.view_as(GoogleCloudOptions).staging_location = staging_dir - self.update_options(options) - options.view_as(SetupOptions).requirements_file = os.path.join( - source_dir, dependency.REQUIREMENTS_FILE) - self.create_temp_file( - os.path.join(source_dir, dependency.REQUIREMENTS_FILE), 'nothing') - self.assertEqual( - sorted([dependency.REQUIREMENTS_FILE, - 'abc.txt', 'def.txt']), - sorted(dependency.stage_job_resources( - options, - populate_requirements_cache=self.populate_requirements_cache))) - self.assertTrue( - os.path.isfile( - os.path.join(staging_dir, dependency.REQUIREMENTS_FILE))) - self.assertTrue(os.path.isfile(os.path.join(staging_dir, 'abc.txt'))) - self.assertTrue(os.path.isfile(os.path.join(staging_dir, 'def.txt'))) + options = PipelineOptions() + options.view_as(GoogleCloudOptions).staging_location = staging_dir + self.update_options(options) + options.view_as(SetupOptions).requirements_cache = requirements_cache_dir + options.view_as(SetupOptions).requirements_file = os.path.join( + source_dir, dependency.REQUIREMENTS_FILE) + self.create_temp_file( + os.path.join(source_dir, dependency.REQUIREMENTS_FILE), 'nothing') + self.assertEqual( + sorted([dependency.REQUIREMENTS_FILE, + 'abc.txt', 'def.txt']), + sorted(dependency.stage_job_resources( + options, + populate_requirements_cache=self.populate_requirements_cache))) + self.assertTrue( + os.path.isfile( + os.path.join(staging_dir, dependency.REQUIREMENTS_FILE))) + self.assertTrue(os.path.isfile(os.path.join(staging_dir, 'abc.txt'))) + self.assertTrue(os.path.isfile(os.path.join(staging_dir, 'def.txt'))) + finally: + shutil.rmtree(staging_dir) + shutil.rmtree(requirements_cache_dir) + shutil.rmtree(source_dir) def test_requirements_file_not_present(self): staging_dir = tempfile.mkdtemp()