forked from pantsbuild/pants
/
jvm_task_test_base.py
64 lines (52 loc) · 2.38 KB
/
jvm_task_test_base.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# coding=utf-8
# Copyright 2015 Pants project contributors (see CONTRIBUTORS.md).
# Licensed under the Apache License, Version 2.0 (see LICENSE).
from __future__ import absolute_import, division, print_function, unicode_literals
import os
from pants.backend.jvm.subsystems.resolve_subsystem import JvmResolveSubsystem
from pants.backend.jvm.tasks.classpath_products import ClasspathProducts
from pants.util.dirutil import safe_file_dump, safe_mkdir, safe_mkdtemp
from pants_test.subsystem.subsystem_util import init_subsystem
from pants_test.task_test_base import TaskTestBase
class JvmTaskTestBase(TaskTestBase):
"""
:API: public
"""
def setUp(self):
"""
:API: public
"""
super(JvmTaskTestBase, self).setUp()
init_subsystem(JvmResolveSubsystem)
self.set_options_for_scope('resolver', resolver='ivy')
def populate_runtime_classpath(self, context, classpath=None):
"""
Helps actual test cases to populate the 'runtime_classpath' products data mapping
in the context, which holds the classpath value for targets.
:API: public
:param context: The execution context where the products data mapping lives.
:param classpath: a list of classpath strings. If not specified,
[os.path.join(self.buildroot, 'none')] will be used.
"""
classpath = classpath or []
runtime_classpath = self.get_runtime_classpath(context)
runtime_classpath.add_for_targets(context.targets(),
[('default', entry) for entry in classpath])
def add_to_runtime_classpath(self, context, tgt, files_dict):
"""Creates and adds the given files to the classpath for the given target under a temp path.
:API: public
"""
runtime_classpath = self.get_runtime_classpath(context)
# Create a temporary directory under the target id, then dump all files.
target_dir = os.path.join(self.test_workdir, tgt.id)
safe_mkdir(target_dir)
classpath_dir = safe_mkdtemp(dir=target_dir)
for rel_path, content in files_dict.items():
safe_file_dump(os.path.join(classpath_dir, rel_path), content, binary_mode=False)
# Add to the classpath.
runtime_classpath.add_for_target(tgt, [('default', classpath_dir)])
def get_runtime_classpath(self, context):
"""
:API: public
"""
return context.products.get_data('runtime_classpath', init_func=ClasspathProducts.init_func(self.pants_workdir))