Skip to content

Commit

Permalink
Detect the PHPUnit version
Browse files Browse the repository at this point in the history
  • Loading branch information
junichi11 committed Apr 22, 2023
1 parent e18e99b commit bcfb8f7
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.netbeans.modules.php.phpunit;

import org.netbeans.api.annotations.common.NonNull;
import org.openide.util.NbBundle;
import org.openide.util.Parameters;

@NbBundle.Messages({
"PhpUnitVersion.PHP_UNIT_9=PHPUnit 9 or earlier",
Expand All @@ -40,10 +42,30 @@ public static PhpUnitVersion getDefault() {
return phpUnitVersions[0];
}

public static PhpUnitVersion fromString(@NonNull String version) {
Parameters.notNull("version", version); // NOI18N
String[] versions = version.split("\\."); // NOI18N
try {
int majorVersion = Integer.parseInt(versions[0]);
if (majorVersion >= 10) {
return PHP_UNIT_10;
} else if (majorVersion <= 9) {
return PHP_UNIT_9;
}
} catch (NumberFormatException ex) {
// no-op
}
return getDefault();
}

public String getDisplayName() {
return displayName;
}

public boolean useNetBeansSuite() {
return this == PHP_UNIT_9;
}

@Override
public String toString() {
return getDisplayName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
import java.util.List;
import java.util.Map;
import java.util.concurrent.CancellationException;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.netbeans.api.annotations.common.CheckForNull;
import org.netbeans.api.annotations.common.NullAllowed;
import org.netbeans.api.extexecution.ExecutionDescriptor;
import org.netbeans.api.extexecution.base.input.InputProcessor;
import org.netbeans.api.extexecution.base.input.InputProcessors;
Expand Down Expand Up @@ -75,6 +78,7 @@
import org.openide.loaders.DataObject;
import org.openide.modules.InstalledFileLocator;
import org.openide.util.NbBundle;
import org.openide.windows.InputOutput;

/**
* PHPUnit 3.4+ support.
Expand Down Expand Up @@ -106,6 +110,7 @@ public final class PhpUnit {
private static final String LIST_GROUPS_PARAM = "--list-groups"; // NOI18N
private static final String GROUP_PARAM = "--group"; // NOI18N
private static final String PARAM_SEPARATOR = "--"; // NOI18N
private static final String VERSION_PARAM = "--version"; // NOI18N
// bootstrap & config
private static final String BOOTSTRAP_PARAM = "--bootstrap"; // NOI18N
private static final String BOOTSTRAP_FILENAME = "bootstrap%s.php"; // NOI18N
Expand Down Expand Up @@ -138,6 +143,8 @@ public final class PhpUnit {
// #200489
private static volatile File suite; // ok if it is fetched more times

private static final ConcurrentMap<PhpModule, PhpUnitVersion> VERSIONS = new ConcurrentHashMap<>();

private final String phpUnitPath;


Expand Down Expand Up @@ -200,6 +207,15 @@ public static PhpUnit getForPhpModule(PhpModule phpModule, boolean showCustomize
return new PhpUnit(path);
}

public static void resetVersions() {
VERSIONS.clear();
}

public static String getVersionLine(String path) {
PhpUnit phpUnit = new PhpUnit(path);
return phpUnit.getVersionLine((PhpModule) null);
}

@CheckForNull
private static String validateDefault() {
ValidationResult result = new PhpUnitOptionsValidator()
Expand Down Expand Up @@ -235,6 +251,51 @@ private static File getNbSuite() {
return suite;
}

@NbBundle.Messages({
"PhpUnit.getting.phpUnit.version=Getting the version...",
"PhpUnit.notFound.phpUnit.version=PHPUnit version not found",
})
public String getVersionLine(@NullAllowed PhpModule phpModule) {
PhpExecutable phpUnit = phpModule != null ? getExecutable(phpModule) : new PhpExecutable(phpUnitPath);
if (phpUnit != null) {
phpUnit.additionalParameters(Arrays.asList(VERSION_PARAM));
final PhpUnitLineProcessor lineProcessor = new PhpUnitLineProcessor();
ExecutionDescriptor silentDescriptor = getSilentDescriptor();
PhpUnitOutputProcessorFactory outputProcessorFactory = new PhpUnitOutputProcessorFactory(lineProcessor);
try {
phpUnit.runAndWait(silentDescriptor, outputProcessorFactory, Bundle.PhpUnit_getting_phpUnit_version());
String version = lineProcessor.getLinesAsText();
return !version.isEmpty() ? version : Bundle.PhpUnit_notFound_phpUnit_version();
} catch (ExecutionException ex) {
LOGGER.log(Level.INFO, null, ex);
}
}
return Bundle.PhpUnit_notFound_phpUnit_version();
}

private PhpUnitVersion getVersion(PhpModule phpModule) {
return getVersion(phpModule, false);
}

private PhpUnitVersion getVersion(PhpModule phpModule, boolean force) {
PhpUnitVersion phpUnitVersion = VERSIONS.get(phpModule);
if (phpUnitVersion != null && !force) {
return phpUnitVersion;
}
String versionLine = getVersionLine(phpModule);
phpUnitVersion = getVersion(versionLine);
VERSIONS.putIfAbsent(phpModule, phpUnitVersion);
return phpUnitVersion;
}

private PhpUnitVersion getVersion(String versionLine) {
String[] versionParts = versionLine.split(" "); // NOI18N e.g. PHPUnit 10.0.1 by ...
if (versionParts.length >= 2) {
return PhpUnitVersion.fromString(versionParts[1]);
}
return PhpUnitVersion.getDefault();
}

@CheckForNull
public Integer runTests(PhpModule phpModule, TestRunInfo runInfo) throws TestRunException {
PhpExecutable phpUnit = getExecutable(phpModule);
Expand Down Expand Up @@ -403,11 +464,12 @@ private TestParams getTestParams(PhpModule phpModule, TestRunInfo runInfo) throw
// only test dir and use 'phpunit' command only
useNbSuite = false;
}
PhpUnitVersion version = getVersion(phpModule);
if (useNbSuite) {
// standard suite
// #218607 - hotfix
//params.add(SUITE_NAME)
if (PhpUnitPreferences.getPhpUnitVersion(phpModule) == PhpUnitVersion.PHP_UNIT_9) {
if (version.useNetBeansSuite()) {
params.add(getNbSuite().getAbsolutePath());
} else {
// GH-5790 we can use NetBeansSuite.php no longer with PHPUnit 10
Expand Down Expand Up @@ -457,6 +519,11 @@ private ExecutionDescriptor getGroupsDescriptor() {
.optionsPath(PhpUnitOptionsPanelController.OPTIONS_PATH);
}

private ExecutionDescriptor getSilentDescriptor() {
return new ExecutionDescriptor()
.inputOutput(InputOutput.NULL);
}

// #170120
@CheckForNull
private File getWorkingDirectory(PhpModule phpModule) {
Expand Down Expand Up @@ -792,6 +859,50 @@ public boolean hasOutput() {

}

private static final class PhpUnitOutputProcessorFactory implements ExecutionDescriptor.InputProcessorFactory2 {

private final LineProcessor lineProcessor;

public PhpUnitOutputProcessorFactory(LineProcessor lineProcessor) {
this.lineProcessor = lineProcessor;
}

@Override
public InputProcessor newInputProcessor(InputProcessor defaultProcessor) {
return InputProcessors.ansiStripping(InputProcessors.bridge(lineProcessor));
}
}

private static final class PhpUnitLineProcessor implements LineProcessor {

private final List<String> lines = new ArrayList<>();

@Override
public void processLine(String line) {
lines.add(line);
}

@Override
public void reset() {
}

@Override
public void close() {
}

public List<String> getLines() {
return Collections.unmodifiableList(lines);
}

public String getLinesAsText() {
StringBuilder sb = new StringBuilder();
for (String string : lines) {
sb.append(string).append("\n"); // NOI18N
}
return sb.toString().trim();
}
}

private static final class TestParams {

private final List<String> params;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,4 @@ CustomizerPhpUnit.scriptLabel.text=PHPUnit Script:
CustomizerPhpUnit.scriptBrowseButton.text=Brow&se...
CustomizerPhpUnit.runPhpUnitOnlyCheckBox.text=&Test Project Using Just 'phpunit' Command
CustomizerPhpUnit.versionLabel.text=PHPUnit &Version:
CustomizerPhpUnit.versionComboBox.AccessibleContext.accessibleDescription=Select PHPUnit version for your project
CustomizerPhpUnit.versionLabel.AccessibleContext.accessibleDescription=PHPUnit version label
CustomizerPhpUnit.versionComboBox.AccessibleContext.accessibleName=PHPUnit Version
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@
<Group type="102" attributes="0">
<Component id="versionLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace max="-2" attributes="0"/>
<Component id="versionComboBox" max="32767" attributes="0"/>
<Component id="versionLineLabel" min="-2" max="-2" attributes="0"/>
<EmptySpace min="0" pref="0" max="32767" attributes="0"/>
</Group>
</Group>
</DimensionLayout>
Expand Down Expand Up @@ -160,7 +161,7 @@
<EmptySpace max="-2" attributes="0"/>
<Group type="103" groupAlignment="3" attributes="0">
<Component id="versionLabel" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="versionComboBox" alignment="3" min="-2" max="-2" attributes="0"/>
<Component id="versionLineLabel" alignment="3" min="-2" max="-2" attributes="0"/>
</Group>
<EmptySpace max="32767" attributes="0"/>
</Group>
Expand Down Expand Up @@ -482,7 +483,7 @@
<Component class="javax.swing.JLabel" name="versionLabel">
<Properties>
<Property name="labelFor" type="java.awt.Component" editor="org.netbeans.modules.form.ComponentChooserEditor">
<ComponentRef name="versionComboBox"/>
<ComponentRef name="default"/>
</Property>
<Property name="text" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/php/phpunit/ui/customizer/Bundle.properties" key="CustomizerPhpUnit.versionLabel.text" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
Expand All @@ -494,23 +495,10 @@
</Property>
</AccessibilityProperties>
</Component>
<Component class="javax.swing.JComboBox" name="versionComboBox">
<Component class="javax.swing.JLabel" name="versionLineLabel">
<Properties>
<Property name="model" type="javax.swing.ComboBoxModel" editor="org.netbeans.modules.form.editors2.ComboBoxModelEditor">
<StringArray count="0"/>
</Property>
<Property name="text" type="java.lang.String" value="VERSION" noResource="true"/>
</Properties>
<AccessibilityProperties>
<Property name="AccessibleContext.accessibleName" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/php/phpunit/ui/customizer/Bundle.properties" key="CustomizerPhpUnit.versionComboBox.AccessibleContext.accessibleName" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
</Property>
<Property name="AccessibleContext.accessibleDescription" type="java.lang.String" editor="org.netbeans.modules.i18n.form.FormI18nStringEditor">
<ResourceString bundle="org/netbeans/modules/php/phpunit/ui/customizer/Bundle.properties" key="CustomizerPhpUnit.versionComboBox.AccessibleContext.accessibleDescription" replaceFormat="org.openide.util.NbBundle.getMessage({sourceFileName}.class, &quot;{key}&quot;)"/>
</Property>
</AccessibilityProperties>
<AuxValues>
<AuxValue name="JavaCodeGenerator_TypeParameters" type="java.lang.String" value="&lt;PhpUnitVersion&gt;"/>
</AuxValues>
</Component>
</SubComponents>
</Form>
Loading

0 comments on commit bcfb8f7

Please sign in to comment.