Skip to content

Commit

Permalink
Add test for misc/extdeps_pomgen (#28)
Browse files Browse the repository at this point in the history
  • Loading branch information
simontoens authored Apr 11, 2020
1 parent 8cec530 commit ab780b6
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 11 deletions.
13 changes: 12 additions & 1 deletion misc/BUILD
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
python_version = 'PY2'
python_version = 'PY3'

py_binary(
name = "extdeps",
srcs = ["//:pomgen_files", "extdeps_pomgen.py"],
main = "extdeps_pomgen.py",
python_version = python_version,
)

py_test(
name = "extdeps_pomgentest",
srcs = ["//:pomgen_files",
"extdeps_pomgen.py",
"tests/extdeps_pomgentest.py"],
imports = ["."],
size = "small",
python_version = python_version,
)

24 changes: 14 additions & 10 deletions misc/extdeps_pomgen.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def _starts_with_ignored_prefix(line):
return True
return False

if __name__ == "__main__":
args = _parse_arguments(sys.argv[1:])
def main(args):
args = _parse_arguments(args)
repo_root = common.get_repo_root(args.repo_root)
cfg = config.load(repo_root)
workspace = workspace.Workspace(repo_root,
cfg.external_dependencies,
cfg.excluded_dependency_paths,
cfg.all_src_exclusions)
ws = workspace.Workspace(repo_root,
cfg.external_dependencies,
cfg.excluded_dependency_paths,
cfg.all_src_exclusions)

group_id = "all_ext_deps_group" if args.group_id is None else args.group_id
artifact_id = "all_ext_deps_art" if args.artifact_id is None else args.artifact_id
Expand All @@ -88,12 +88,16 @@ def _starts_with_ignored_prefix(line):
if _starts_with_ignored_prefix(line):
continue
dep_labels.append(line)
unique_dependencies = set(workspace.parse_dep_labels(dep_labels))
unique_dependencies = set(ws.parse_dep_labels(dep_labels))
dependencies = list(unique_dependencies)
else:
dependencies = list(workspace.name_to_external_dependencies.values())
dependencies = list(ws.name_to_external_dependencies.values())

dependencies.sort()
pomgen = ThirdPartyDepsPomGen(workspace, artifact_def, dependencies, cfg.pom_template)
pomgen = ThirdPartyDepsPomGen(ws, artifact_def, dependencies,
cfg.pom_template)
pomgen.process_dependencies()
print(pomgen.gen())
return pomgen.gen()

if __name__ == "__main__":
print(main(sys.argv[1:]))
54 changes: 54 additions & 0 deletions misc/tests/extdeps_pomgentest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
"""
Copyright (c) 2018, salesforce.com, inc.
All rights reserved.
SPDX-License-Identifier: BSD-3-Clause
For full license text, see the LICENSE file in the repo root or https://opensource.org/licenses/BSD-3-Clause
"""

import extdeps_pomgen
import os
import tempfile
import unittest

class ExtDepsPomgenTest(unittest.TestCase):

def test1(self):
self._setup_workspace()
args = ("--repo_root", self.repo_root_path,)

pom = extdeps_pomgen.main(args)

self.assertIn("<groupId>com.google.guava</groupId>", pom)
self.assertIn("<artifactId>guava</artifactId>", pom)
self.assertIn("<version>23.0</version", pom)

def _setup_workspace(self):
self.repo_root_path = tempfile.mkdtemp("monorepo")
self._add_WORKSPACE_file()
self._add_pom_template()

def _add_WORKSPACE_file(self):
content = """
maven_jar(
name = "com_google_guava_guava",
artifact = "com.google.guava:guava:23.0",
)
"""
self._write_file("", "", "WORKSPACE", content)

def _add_pom_template(self):
content = "${dependencies}"
self._write_file("config", "", "pom_template.xml", content)

def _write_file(self, package_rel_path, rel_path, filename, content):
print(content)
path = os.path.join(self.repo_root_path, package_rel_path, rel_path,
filename)
parent_dir = os.path.dirname(path)
if not os.path.exists(parent_dir):
os.makedirs(parent_dir)
with open(path, "w") as f:
f.write(content)

if __name__ == '__main__':
unittest.main()

0 comments on commit ab780b6

Please sign in to comment.