From e9cfaf8fb00e96853ef2d981c55f3c2f98d83aa4 Mon Sep 17 00:00:00 2001 From: Eric Milles Date: Sat, 4 Dec 2021 22:08:35 -0600 Subject: [PATCH] Fix for #1299: re-apply project classpath to saved script launch config --- .../GroovyScriptLaunchShortcutTests.groovy | 28 ++++++++++++++++++- .../AbstractGroovyLaunchShortcut.java | 20 +++++++------ 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/launch/GroovyScriptLaunchShortcutTests.groovy b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/launch/GroovyScriptLaunchShortcutTests.groovy index 8127e7ef1d..992ff825b8 100644 --- a/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/launch/GroovyScriptLaunchShortcutTests.groovy +++ b/ide-test/org.codehaus.groovy.eclipse.tests/src/org/codehaus/groovy/eclipse/test/launch/GroovyScriptLaunchShortcutTests.groovy @@ -1,5 +1,5 @@ /* - * Copyright 2009-2020 the original author or authors. + * Copyright 2009-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -24,6 +24,7 @@ import org.eclipse.core.resources.IFile import org.eclipse.core.resources.IncrementalProjectBuilder import org.eclipse.core.resources.ResourcesPlugin import org.eclipse.core.runtime.Adapters +import org.eclipse.debug.core.DebugPlugin import org.eclipse.debug.internal.ui.DebugUIPlugin import org.eclipse.debug.internal.ui.IInternalDebugUIConstants import org.eclipse.debug.internal.ui.preferences.IDebugPreferenceConstants @@ -51,6 +52,9 @@ final class GroovyScriptLaunchShortcutTests extends GroovyEclipseTestSuite { setToDefault(IDebugPreferenceConstants.CONSOLE_OPEN_ON_OUT) setToDefault(IInternalDebugUIConstants.PREF_WAIT_FOR_BUILD) } + + def configType = new GroovyScriptLaunchShortcut().groovyLaunchConfigType + DebugPlugin.getDefault().launchManager.getLaunchConfigurations(configType).each { it.delete() } } @Test @@ -288,6 +292,28 @@ final class GroovyScriptLaunchShortcutTests extends GroovyEclipseTestSuite { launchScriptAndAssertExitValue(type) } + @Test // https://github.com/groovy/groovy-eclipse/issues/1299 + void testScriptLaunch15() { + def shortcut = new GroovyScriptLaunchShortcut() + def unitType = addGroovySource('print "hello"', 'script').getType('script') + + def newLaunchConfig = { -> + def workCopy = shortcut.findOrCreateLaunchConfig(shortcut.createLaunchProperties(unitType, unitType.javaProject), unitType.fullyQualifiedName) + try { + String arguments = workCopy.getAttribute(IJavaLaunchConfigurationConstants.ATTR_PROGRAM_ARGUMENTS, '') + return arguments + } finally { + workCopy.doSave() + } + } + + assert !newLaunchConfig().contains('jupiter') + + addJUnit(5) // change the project's classpath + + assert newLaunchConfig().contains('jupiter') + } + //-------------------------------------------------------------------------- private String buildScriptClasspath(IJavaProject javaProject) { diff --git a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/launchers/AbstractGroovyLaunchShortcut.java b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/launchers/AbstractGroovyLaunchShortcut.java index 00e20438fa..4f4dc3dc4b 100644 --- a/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/launchers/AbstractGroovyLaunchShortcut.java +++ b/ide/org.codehaus.groovy.eclipse.ui/src/org/codehaus/groovy/eclipse/launchers/AbstractGroovyLaunchShortcut.java @@ -1,5 +1,5 @@ /* - * Copyright 2009-2020 the original author or authors. + * Copyright 2009-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -354,22 +354,26 @@ private String getWorkingDirectory(final IType runType, final IJavaProject javaP * creates one. */ protected ILaunchConfigurationWorkingCopy findOrCreateLaunchConfig(final Map launchProperties, final String launchName) throws CoreException { - String projectName = launchProperties.get(ATTR_PROJECT_NAME); - ILaunchConfiguration config = findConfiguration(projectName, launchProperties.get(GROOVY_TYPE_TO_RUN)); - return (config != null ? config.getWorkingCopy() : createLaunchConfig(launchProperties, launchName)); + ILaunchConfiguration config = findConfiguration(launchProperties.get(ATTR_PROJECT_NAME), launchProperties.get(GROOVY_TYPE_TO_RUN)); + if (config == null) { + return createLaunchConfig(launchProperties, launchName); + } else { + ILaunchConfigurationWorkingCopy workingCopy = config.getWorkingCopy(); + // program arguments contains project's classpath, so always use fresh value + workingCopy.setAttribute(ATTR_PROGRAM_ARGUMENTS, launchProperties.get(ATTR_PROGRAM_ARGUMENTS)); + + return workingCopy; + } } /** * Creates a launch configuration for the given name and properties. */ private ILaunchConfigurationWorkingCopy createLaunchConfig(final Map launchProperties, final String launchName) throws CoreException { - ILaunchConfigurationWorkingCopy workingCopy = getGroovyLaunchConfigType().newInstance( - null, getLaunchManager().generateLaunchConfigurationName(launchName)); - + ILaunchConfigurationWorkingCopy workingCopy = getGroovyLaunchConfigType().newInstance(null, getLaunchManager().generateLaunchConfigurationName(launchName)); for (Map.Entry entry : launchProperties.entrySet()) { workingCopy.setAttribute(entry.getKey(), entry.getValue()); } - return workingCopy; }