Skip to content

Commit

Permalink
Merge pull request #97 from jayvdb/sort-jars
Browse files Browse the repository at this point in the history
Sort jars before searching for mainClass
  • Loading branch information
ctrueden authored Jul 22, 2024
2 parents 35e301f + 07c9a90 commit 6c92065
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
14 changes: 10 additions & 4 deletions src/jgo/jgo.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,12 +821,18 @@ def _run(
pass

if not primary_endpoint.main_class:
_logger.info("Inferring main class from jar manifest")
jar_path = glob.glob(
os.path.join(workspace, primary_endpoint.jar_name())
glob_pattern = (
primary_endpoint.jar_name()
.replace(Endpoint.VERSION_RELEASE, "*")
.replace(Endpoint.VERSION_LATEST, "*")
)[0]
)
_logger.info(
f"Inferring main class from manifest of {primary_endpoint.jar_name()}"
f" using glob '{glob_pattern}'"
)
jar_paths = sorted(glob.glob(os.path.join(workspace, glob_pattern)))
_logger.debug(f"Using first jar from matching jars {jar_paths}")
jar_path = jar_paths[0]
with zipfile.ZipFile(jar_path) as jar_file:
with jar_file.open("META-INF/MANIFEST.MF") as manifest:
main_class_pattern = re.compile(".*Main-Class: *")
Expand Down
23 changes: 23 additions & 0 deletions tests/test_run.py
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,29 @@ def test_explicit_main_class(self, launch_java_mock):
self.assertIsNone(stderr)
self.assertFalse(check)

@patch("jgo.jgo.launch_java")
def test_infer_main_class(self, launch_java_mock):
parser = jgo_parser()
argv = ["com.pinterest.ktlint:ktlint-cli"]

run(parser, argv)
self.assertTrue(launch_java_mock.called)
args, kwargs = _call_args(launch_java_mock)
workspace = args[0]
jvm_args = args[1]
program_args = args[2:]
additional_jars = kwargs["additional_jars"]
stdout = kwargs["stdout"]
stderr = kwargs["stderr"]
check = kwargs["check"]
self.assertIsInstance(workspace, str)
self.assertEqual(jvm_args, [])
self.assertEqual(program_args, ("com.pinterest.ktlint.Main",))
self.assertEqual(additional_jars, [])
self.assertIsNone(stdout)
self.assertIsNone(stderr)
self.assertFalse(check)


class TestUtil(unittest.TestCase):
@patch("jgo.jgo._run")
Expand Down

0 comments on commit 6c92065

Please sign in to comment.