Skip to content

Commit 1de38eb

Browse files
committed
Remove TemporaryDirectory inside another TemporaryDirectory.
Signed-off-by: dblock <[email protected]>
1 parent 36dff3b commit 1de38eb

File tree

5 files changed

+27
-28
lines changed

5 files changed

+27
-28
lines changed

src/assemble_workflow/bundle.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __enter__(self):
2929
def __exit__(self, exc_type, exc_value, exc_traceback):
3030
self.tmp_dir.__exit__(exc_type, exc_value, exc_traceback)
3131

32-
def __init__(self, build_manifest, artifacts_dir, bundle_recorder):
32+
def __init__(self, build_manifest, artifacts_dir, bundle_recorder, keep=False):
3333
"""
3434
Construct a new Bundle instance.
3535
:param build_manifest: A BuildManifest created from the build workflow.
@@ -39,7 +39,7 @@ def __init__(self, build_manifest, artifacts_dir, bundle_recorder):
3939
self.plugins = self.__get_plugins(build_manifest.components.values())
4040
self.artifacts_dir = artifacts_dir
4141
self.bundle_recorder = bundle_recorder
42-
self.tmp_dir = TemporaryDirectory()
42+
self.tmp_dir = TemporaryDirectory(keep=keep)
4343
self.min_dist = self.__get_min_dist(build_manifest.components.values())
4444
self.installed_plugins = []
4545

src/assemble_workflow/bundles.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ def from_name(cls, name):
2222
return klass
2323

2424
@classmethod
25-
def create(cls, build_manifest, artifacts_dir, bundle_recorder):
25+
def create(cls, build_manifest, artifacts_dir, bundle_recorder, keep):
2626
klass = cls.from_name(build_manifest.build.name)
27-
return klass(build_manifest, artifacts_dir, bundle_recorder)
27+
return klass(build_manifest, artifacts_dir, bundle_recorder, keep)

src/run_assemble.py

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
from assemble_workflow.bundles import Bundles
1616
from manifests.build_manifest import BuildManifest
1717
from system import console
18-
from system.temporary_directory import TemporaryDirectory
1918

2019

2120
def main():
@@ -29,21 +28,20 @@ def main():
2928
output_dir = os.path.join(os.getcwd(), "dist")
3029
os.makedirs(output_dir, exist_ok=True)
3130

32-
with TemporaryDirectory(chdir=True, keep=args.keep):
33-
logging.info(f"Bundling {build.name} ({build.architecture}) on {build.platform} into {output_dir} ...")
31+
logging.info(f"Bundling {build.name} ({build.architecture}) on {build.platform} into {output_dir} ...")
3432

35-
bundle_recorder = BundleRecorder(build, output_dir, artifacts_dir, args.base_url)
33+
bundle_recorder = BundleRecorder(build, output_dir, artifacts_dir, args.base_url)
3634

37-
with Bundles.create(build_manifest, artifacts_dir, bundle_recorder) as bundle:
38-
bundle.install_min()
39-
bundle.install_plugins()
40-
logging.info(f"Installed plugins: {bundle.installed_plugins}")
35+
with Bundles.create(build_manifest, artifacts_dir, bundle_recorder, args.keep) as bundle:
36+
bundle.install_min()
37+
bundle.install_plugins()
38+
logging.info(f"Installed plugins: {bundle.installed_plugins}")
4139

42-
# Save a copy of the manifest inside of the tar
43-
bundle_recorder.write_manifest(bundle.min_dist.archive_path)
44-
bundle.package(output_dir)
40+
# Save a copy of the manifest inside of the tar
41+
bundle_recorder.write_manifest(bundle.min_dist.archive_path)
42+
bundle.package(output_dir)
4543

46-
bundle_recorder.write_manifest(output_dir)
44+
bundle_recorder.write_manifest(output_dir)
4745

4846
logging.info("Done.")
4947

tests/test_run_assemble.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
# compatible open source license.
66

77
import os
8-
import tempfile
98
import unittest
109
from unittest.mock import MagicMock, call, patch
1110

@@ -31,14 +30,12 @@ def test_usage(self, *mocks):
3130

3231
@patch("os.chdir")
3332
@patch("os.makedirs")
33+
@patch("shutil.copy2")
3434
@patch("os.getcwd", return_value="curdir")
3535
@patch("argparse._sys.argv", ["run_assemble.py", BUILD_MANIFEST])
3636
@patch("run_assemble.Bundles.create")
3737
@patch("run_assemble.BundleRecorder", return_value=MagicMock())
38-
@patch("run_assemble.TemporaryDirectory")
39-
@patch("shutil.copy2")
40-
def test_main(self, mock_copy, mock_temp, mock_recorder, mock_bundles, *mocks):
41-
mock_temp.return_value.__enter__.return_value.name = tempfile.gettempdir()
38+
def test_main(self, mock_recorder, mock_bundles, *mocks):
4239
mock_bundle = MagicMock(min_dist=MagicMock(archive_path="path"))
4340
mock_bundles.return_value.__enter__.return_value = mock_bundle
4441

@@ -49,7 +46,4 @@ def test_main(self, mock_copy, mock_temp, mock_recorder, mock_bundles, *mocks):
4946

5047
mock_bundle.package.assert_called_with(os.path.join("curdir", "dist"))
5148

52-
mock_recorder.return_value.write_manifest.assert_has_calls([
53-
call("path"),
54-
call(os.path.join("curdir", "dist"))
55-
]) # manifest included in package
49+
mock_recorder.return_value.write_manifest.assert_has_calls([call("path"), call(os.path.join("curdir", "dist"))]) # manifest included in package

tests/tests_assemble_workflow/test_bundles.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,21 @@ class TestBundles(unittest.TestCase):
1818
def test_bundle_opensearch(self):
1919
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
2020
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
21-
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())
21+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), False)
2222
self.assertIs(type(bundle), BundleOpenSearch)
2323

2424
def test_bundle_opensearch_dashboards(self):
2525
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-dashboards-build-1.1.0.yml")
2626
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
27-
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock())
27+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), False)
2828
self.assertIs(type(bundle), BundleOpenSearchDashboards)
29+
self.assertFalse(bundle.tmp_dir.keep)
30+
31+
def test_bundle_keep(self):
32+
manifest_path = os.path.join(os.path.dirname(__file__), "data", "opensearch-build-linux-1.1.0.yml")
33+
artifacts_path = os.path.join(os.path.dirname(__file__), "data", "artifacts")
34+
bundle = Bundles.create(BuildManifest.from_path(manifest_path), artifacts_path, MagicMock(), True)
35+
self.assertTrue(bundle.tmp_dir.keep)
2936

3037
def test_bundle_opensearch_invalid(self):
3138
manifest = BuildManifest(
@@ -41,5 +48,5 @@ def test_bundle_opensearch_invalid(self):
4148
}
4249
)
4350
with self.assertRaises(ValueError) as ctx:
44-
Bundles.create(manifest, "path", MagicMock())
51+
Bundles.create(manifest, "path", MagicMock(), False)
4552
self.assertEqual(str(ctx.exception), "Unsupported bundle: invalid")

0 commit comments

Comments
 (0)