Skip to content

Commit

Permalink
Fix getFullPath() in stub classes, fix PythonModelProviderTest.
Browse files Browse the repository at this point in the history
  • Loading branch information
fabioz committed Sep 1, 2024
1 parent 072257a commit fedfae4
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,10 @@ public InputStream getContents(boolean force) throws CoreException {
public IPath getFullPath() {
IPath projectPath = project.getLocation();
IPath filePath = Path.fromOSString(FileUtils.getFileAbsolutePath(file));
return filePath.makeRelativeTo(projectPath);
IPath relativeToProject = filePath.makeRelativeTo(projectPath);
// Important: the full path is relative to the workspace, so, we need to
// add the project there too.
return new Path(this.project.getName()).append(relativeToProject);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ public IContainer getParent() {
if (parent != null) {
return parent;
}
return new FolderStub(this.project, this.folder.getParentFile());
File parentFile = this.folder.getParentFile();
if (parentFile == null) {
return null;
}
if (Path.fromOSString(FileUtils.getFileAbsolutePath(parentFile)).equals(project.getLocation())) {
return project;
}

return new FolderStub(this.project, parentFile);
}

@Override
Expand Down Expand Up @@ -122,8 +130,13 @@ public IPath getFullPath() {
String fileAbsolutePath = FileUtils.getFileAbsolutePath(this.folder);

IPath fromOSString = Path.fromOSString(fileAbsolutePath);
IPath workspace = this.project.getLocation();
return fromOSString.makeRelativeTo(workspace);
IPath project = this.project.getLocation();
IPath relativeToProject = fromOSString.makeRelativeTo(project);

// Important: the full path is relative to the workspace, so, we need to
// add the project there too.
return new Path(this.project.getName()).append(relativeToProject);

}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
Expand Down Expand Up @@ -82,6 +83,26 @@ public IResource getResource(File parentFile) {
return r;
}

@Override
public int hashCode() {
return Objects.hash(projectRoot);
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
ProjectStub other = (ProjectStub) obj;
return Objects.equals(projectRoot, other.projectRoot);
}

public IContainer getFolder(File parentFile) {
return (IContainer) getResource(parentFile);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,17 @@ protected void tearDown() throws Exception {
* from the python model.
*/
public void testInterceptAdd() throws Exception {
PythonNature nature = createNature(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python");
String pythonpathLoc = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python";
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(pythonpathLoc);

PythonNature nature = createNature(pythonpathLoc);

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
file = new FileStub(project, new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC
+ "projroot/source/python/pack1/pack2/mod2.py"));
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

HashSet<Object> files = new HashSet<Object>();
files.add(file);
Expand All @@ -99,10 +104,15 @@ public void testInterceptAdd() throws Exception {
* Test if intercepting an object that does not have a parent works.
*/
public void testInterceptRefresh() throws Exception {
PythonNature nature = createNature(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python");
String pythonpathLoc = TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source/python";
final HashSet<String> pythonPathSet = new HashSet<String>();
pythonPathSet.add(pythonpathLoc);

PythonNature nature = createNature(pythonpathLoc);

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

PipelinedViewerUpdate update = new PipelinedViewerUpdate();
Set<Object> refreshTargets = update.getRefreshTargets();
Expand All @@ -129,6 +139,8 @@ public void testProjectIsRoot2() throws Exception {
WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
project = new ProjectStub(new File(pythonpathLoc), nature);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

FolderStub folder = new FolderStub(project,
new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot/source"));

Expand Down Expand Up @@ -159,6 +171,7 @@ public void testProjectIsRoot() throws Exception {
WorkspaceRootStub workspaceRootStub = new WorkspaceRootStub();
project = new ProjectStub(new File(pythonpathLoc), nature);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

workspaceRootStub.addChild(project);
workspaceRootStub.addChild(null);
Expand Down Expand Up @@ -264,6 +277,8 @@ public void testPythonpathChanges() throws Exception {

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, true);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

Object[] children1 = provider.getChildren(project);
assertTrue(children1[0] instanceof PythonSourceFolder);

Expand Down Expand Up @@ -296,6 +311,7 @@ public void testDontRemoveOtherPluginElements() throws Exception {

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

HashSet<Object> currentChildren = new HashSet<Object>();
currentChildren.add("Test");
Expand Down Expand Up @@ -336,6 +352,7 @@ public void testCreateChildrenInWrappedResource() throws Exception {
project.setParent(workspaceRootStub);

provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

HashSet<Object> currentChildren = new HashSet<Object>();
currentChildren.add(project);
Expand Down Expand Up @@ -363,6 +380,7 @@ public void testNullElements() throws Exception {
project.setParent(workspaceRootStub);

provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

HashSet<Object> currentChildren = new HashSet<Object>();
currentChildren.add(project);
Expand Down Expand Up @@ -395,6 +413,7 @@ public void testAddSourceFolderToSourceFolder() throws Exception {

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));
Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue(children1[0] instanceof PythonSourceFolder);
Expand Down Expand Up @@ -434,6 +453,8 @@ public void testFolderToSourceFolder() throws Exception {

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue("Found: " + children1[0], children1[0] instanceof PythonSourceFolder);
Expand Down Expand Up @@ -491,6 +512,8 @@ public void testFolderToSourceFolder2() throws Exception {

project = new ProjectStub(new File(TestDependent.TEST_PYSRC_NAVIGATOR_LOC + "projroot"), nature, false);
provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

Object[] children1 = provider.getChildren(project);
assertEquals(1, children1.length);
assertTrue("Expected source folder. Received: " + children1[0], children1[0] instanceof PythonSourceFolder);
Expand Down Expand Up @@ -567,6 +590,8 @@ public List<IWorkingSet> call(IWorkspaceRoot arg) {
};

provider = new PythonModelProvider();
provider.internalDoNotifyPythonPathRebuilt(project, new ArrayList<String>(pythonPathSet));

provider.topLevelChoice.rootMode = TopLevelProjectsOrWorkingSetChoice.WORKING_SETS;

//--- check children for the workspace (projects changed for working sets)
Expand Down

0 comments on commit fedfae4

Please sign in to comment.