Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: NPE when get test source paths #1622

Merged
merged 1 commit into from
Oct 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ hs_err_pid*
.DS_Store
target/
.settings
test/**/.classpath
test/**/.project
**/.checkstyle
target/
bin/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2017-2021 Microsoft Corporation and others.
* Copyright (c) 2017-2023 Microsoft Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
Expand All @@ -11,6 +11,7 @@

package com.microsoft.java.test.plugin.util;

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jdt.core.IClasspathAttribute;
Expand Down Expand Up @@ -111,13 +112,13 @@ public static List<TestSourcePath> getTestSourcePaths(IJavaProject project) thro
}

if (isTestEntry(entry)) {
paths.add(new TestSourcePath(parseTestSourcePathString(entry, project), true));
addTestSourcePath(paths, project.getProject(), entry, true);
continue;
}

// Always return true Eclipse & invisible project
if (ProjectUtils.isGeneralJavaProject(project.getProject())) {
paths.add(new TestSourcePath(parseTestSourcePathString(entry, project), false));
addTestSourcePath(paths, project.getProject(), entry, false);
}
}
return paths;
Expand All @@ -134,14 +135,20 @@ public static Set<IJavaProject> parseProjects(String uriStr) {
.collect(Collectors.toSet());
}

private static String parseTestSourcePathString(IClasspathEntry entry, IJavaProject project) {
final IPath relativePath = entry.getPath().makeRelativeTo(project.getPath());
private static void addTestSourcePath(List<TestSourcePath> paths, IProject project,
IClasspathEntry entry, boolean isStrict) {
final IPath relativePath = entry.getPath().makeRelativeTo(project.getFullPath());
final IPath testSourcePath;
// Eclipse project that source files stored directly at project root
if (relativePath.isEmpty()) {
return project.getProject().getLocation().toOSString();
testSourcePath = project.getLocation();
} else {
testSourcePath = project.getFolder(relativePath).getLocation();
}

return project.getProject().getFolder(relativePath).getLocation().toOSString();
if (testSourcePath != null) {
paths.add(new TestSourcePath(testSourcePath.toOSString(), isStrict));
}
}

public static boolean isTest(IJavaProject project, IPath path, boolean containsGeneral) {
Expand Down
Loading