Skip to content

Commit 5f1c5d3

Browse files
authored
Remove the confusing "dummy_test_module" directory (spotify#1756)
The purpose is simply to remove a confusing module from the luigi root directory. It was only used from tests but it couldn't be put in the test module for whatever technical reasons.
1 parent e3cc470 commit 5f1c5d3

File tree

7 files changed

+50
-32
lines changed

7 files changed

+50
-32
lines changed

.coveragerc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
[report]
22
omit =
33
luigi/mrrunner.py
4+
test/_test_time_generated_module*.py
45
*/python?.?/*
56
*/site-packages/nose/*
67
*__init__*
@@ -9,7 +10,6 @@ omit =
910
*/.tox/*
1011
*/setup.py
1112
*/bin/luigidc
12-
*/dummy_test_module/*
1313
sitecustomize.py
1414
hadoop_test.py
1515
minicluster.py

dummy_test_module/__init__.py

Whitespace-only changes.

dummy_test_module/not_imported.py

-6
This file was deleted.

test/contrib/spark_test.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@
1515
# limitations under the License.
1616
#
1717

18-
from helpers import unittest
18+
import unittest
1919
import os
2020
import luigi
2121
import luigi.contrib.hdfs
2222
from luigi import six
2323
from luigi.mock import MockTarget
24-
from helpers import with_config
24+
from helpers import with_config, temporary_unloaded_module
2525
from luigi.contrib.external_program import ExternalProgramRunError
2626
from luigi.contrib.spark import SparkSubmitTask, PySparkTask
2727
from mock import patch, call, MagicMock
@@ -197,7 +197,6 @@ def test_run(self, proc):
197197
self.assertTrue(os.path.exists(proc_arg_list[7]))
198198
self.assertTrue(proc_arg_list[8].endswith('TestPySparkTask.pickle'))
199199

200-
@with_config({'spark': {'py-packages': 'dummy_test_module'}})
201200
@patch.dict('sys.modules', {'pyspark': MagicMock()})
202201
@patch('pyspark.SparkContext')
203202
def test_pyspark_runner(self, spark_context):
@@ -211,7 +210,8 @@ def mock_spark_submit(task):
211210

212211
with patch.object(SparkSubmitTask, 'run', mock_spark_submit):
213212
job = TestPySparkTask()
214-
job.run()
213+
with temporary_unloaded_module(b'') as task_module:
214+
with_config({'spark': {'py-packages': task_module}})(job.run)()
215215

216216
sc.textFile.assert_called_with('input')
217217
sc.textFile.return_value.saveAsTextFile.assert_called_with('output')

test/dynamic_import_test.py

+9-16
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,25 @@
1515
# limitations under the License.
1616
#
1717

18-
from helpers import LuigiTestCase
18+
from helpers import LuigiTestCase, temporary_unloaded_module
1919

2020
import luigi
2121
import luigi.interface
22-
import tempfile
23-
import re
2422

25-
_testing_glob_var = None
26-
27-
28-
class CmdlineTest(LuigiTestCase):
29-
30-
def test_dynamic_loading(self):
31-
with tempfile.NamedTemporaryFile(dir='test/', prefix="_foo_module", suffix='.py') as temp_module_file:
32-
temp_module_file.file.write(b'''
23+
CONTENTS = b'''
3324
import luigi
3425
3526
class FooTask(luigi.Task):
3627
x = luigi.IntParameter()
3728
3829
def run(self):
3930
luigi._testing_glob_var = self.x
40-
''')
41-
temp_module_file.file.flush()
42-
temp_module_path = temp_module_file.name
43-
temp_module_name = re.search(r'/(_foo_module.*).py', temp_module_path).group(1)
44-
luigi.interface.run(['--module', temp_module_name, 'FooTask', '--x', '123', '--local-scheduler', '--no-lock'])
31+
'''
4532

33+
34+
class CmdlineTest(LuigiTestCase):
35+
36+
def test_dynamic_loading(self):
37+
with temporary_unloaded_module(CONTENTS) as temp_module_name:
38+
luigi.interface.run(['--module', temp_module_name, 'FooTask', '--x', '123', '--local-scheduler', '--no-lock'])
4639
self.assertEqual(luigi._testing_glob_var, 123)

test/helpers.py

+21
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
import functools
1919
import itertools
20+
import tempfile
21+
import re
22+
from contextlib import contextmanager
2023

2124
import luigi
2225
import luigi.task_register
@@ -184,3 +187,21 @@ def wrapper(*args, **kwargs):
184187
def in_parse(cmds, deferred_computation):
185188
with CmdlineParser.global_instance(cmds) as cp:
186189
deferred_computation(cp.get_task_obj())
190+
191+
192+
@contextmanager
193+
def temporary_unloaded_module(python_file_contents):
194+
""" Create an importable module
195+
196+
Return the name of importable module name given its file contents (source
197+
code) """
198+
with tempfile.NamedTemporaryFile(
199+
dir='test/',
200+
prefix="_test_time_generated_module",
201+
suffix='.py') as temp_module_file:
202+
temp_module_file.file.write(python_file_contents)
203+
temp_module_file.file.flush()
204+
temp_module_path = temp_module_file.name
205+
temp_module_name = re.search(r'/(_test_time_generated_module.*).py',
206+
temp_module_path).group(1)
207+
yield temp_module_name

test/worker_test.py

+15-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@
2525
import threading
2626
import time
2727
import psutil
28-
from helpers import unittest, with_config, skipOnTravis, LuigiTestCase
28+
from helpers import (unittest, with_config, skipOnTravis, LuigiTestCase,
29+
temporary_unloaded_module)
2930

3031
import luigi.notifications
3132
import luigi.worker
@@ -1098,6 +1099,15 @@ class Dummy3Task(Dummy2Task):
10981099
self.assertEqual(list(self.sch.task_list('FAILED', '').keys()), [d.task_id])
10991100

11001101
def test_unimported_job_type(self):
1102+
MODULE_CONTENTS = b'''
1103+
import luigi
1104+
1105+
1106+
class UnimportedTask(luigi.Task):
1107+
def complete(self):
1108+
return False
1109+
'''
1110+
11011111
class NotImportedTask(luigi.Task):
11021112
task_family = 'UnimportedTask'
11031113
task_module = None
@@ -1110,10 +1120,10 @@ class NotImportedTask(luigi.Task):
11101120
self.assertEqual(list(self.sch.task_list('FAILED', '').keys()), [task.task_id])
11111121

11121122
# check that it can import with the right module
1113-
task.task_module = 'dummy_test_module.not_imported'
1114-
self.w.add(task)
1115-
self.assertTrue(self.assistant.run())
1116-
self.assertEqual(list(self.sch.task_list('DONE', '').keys()), [task.task_id])
1123+
with temporary_unloaded_module(MODULE_CONTENTS) as task.task_module:
1124+
self.w.add(task)
1125+
self.assertTrue(self.assistant.run())
1126+
self.assertEqual(list(self.sch.task_list('DONE', '').keys()), [task.task_id])
11171127

11181128

11191129
class ForkBombTask(luigi.Task):

0 commit comments

Comments
 (0)