Skip to content

Commit

Permalink
Fix bug on spaces in imports + add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jecisc committed Oct 2, 2024
1 parent 1f30cca commit 755cf43
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 6 deletions.
25 changes: 25 additions & 0 deletions src/Famix-Python-Importer-Tests/FamixPythonProject1Test.class.st
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,19 @@ FamixPythonProject1Test >> testModuleImportsGlobalVariableFromAModuleRelatively
self assert: import importedEntity equals: global
]

{ #category : 'tests - from-import relative' }
FamixPythonProject1Test >> testModuleImportsGlobalVariableFromAModuleRelativelyWithSpacesInPath [

| module global import |
self denyEmpty: self model allImports.
module := self moduleNamed: 'moduleInSubSubPackage3'.
global := self globalVariableNamed: 'moduleInSubpackage31Variable'.
import := module outgoingImports detect: [ :pimport | pimport target name = 'moduleInSubpackage31Variable' ].

self assert: import importingEntity equals: module.
self assert: import importedEntity equals: global
]

{ #category : 'tests - from-import' }
FamixPythonProject1Test >> testModuleImportsGlobalVariableFromAPackage [

Expand All @@ -541,6 +554,18 @@ FamixPythonProject1Test >> testModuleImportsModuleInPackage [
self assert: import importedEntity equals: importedModule
]

{ #category : 'tests - imports' }
FamixPythonProject1Test >> testModuleImportsSubPackageWithSpacesInThePath [

| moduleAtRoot import |
self denyEmpty: self model allImports.
moduleAtRoot := self moduleNamed: 'moduleWithSpacedImports'.
import := moduleAtRoot outgoingImports detect: [ :pimport | pimport target name = 'subsubpackage1' ].

self assert: import importingEntity equals: moduleAtRoot.
self assert: import importedEntity equals: (self packageNamed: 'subsubpackage1')
]

{ #category : 'tests - modules' }
FamixPythonProject1Test >> testModuleInRootPackage [

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ FamixPythonAbstractImportResolvable >> entity: anObject [
FamixPythonAbstractImportResolvable >> findTargetEntityFor: aPath from: startEntities [

| possibleEntities splittedPath |
possibleEntities := startEntities.
splittedPath := aPath splitOn: $..
possibleEntities := possibleEntities select: [ :possibleEntity | possibleEntity name = splittedPath first ].
"We need to trim because we can have spaces between the dots."
splittedPath := (aPath splitOn: $.) collect: [ :segment | segment trim ].
possibleEntities := startEntities select: [ :possibleEntity | possibleEntity name = splittedPath first ].
splittedPath removeFirst.
[ splittedPath isNotEmpty ] whileTrue: [
| childName |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ FamixPythonFromImportResolvable >> resolveInScope: aScope currentEntity: current
currentPackage := currentPackage parentPackage.
remainingPath := remainingPath allButFirst ].
possibleEntities := currentPackage children ]
ifFalse: [
"If we have an absolute path we just start at the root."
ifFalse: [ "If we have an absolute path we just start at the root."
remainingPath := path.
possibleEntities := currentEntity mooseModel rootEntities ].

possibleEntities := (self findTargetEntityFor: remainingPath from: possibleEntities) allChildren select: [ :child |
"We trim the path because we can have spaces after the dots in relativ paths."
possibleEntities := (self findTargetEntityFor: remainingPath trim from: possibleEntities) allChildren select: [ :child |
child isNamedEntity and: [ child name = entityName ] ].

"It is possible that we have multiple entities with the same name and in that case, python select the last one declared. So we sort by source anchor position to find this one."
Expand Down

0 comments on commit 755cf43

Please sign in to comment.