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

Changed the way we match mappings in NameToInternalName, modified and added new testcases #199

Merged
merged 3 commits into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
81 changes: 72 additions & 9 deletions cls/SourceControl/Git/Utils.cls
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ ClassMethod AddToSourceControl(InternalName As %String) As %Status
set ec = $$$ADDSC(ec, sc)
}
for i=1:1:filenames{
set FileInternalName = ##class(SourceControl.Git.Utils).NormalizeExtension(##class(SourceControl.Git.Utils).NameToInternalName(filenames(i), 0))
set FileInternalName = ##class(SourceControl.Git.Utils).NormalizeExtension(##class(SourceControl.Git.Utils).NameToInternalName(filenames(i), 0,,1))
set FileType = ##class(SourceControl.Git.Utils).Type(FileInternalName)

set @..#Storage@("items", FileInternalName) = ""
Expand Down Expand Up @@ -1530,7 +1530,7 @@ ClassMethod Name(InternalName As %String, ByRef MappingExists As %Boolean) As %S
NameToInternalName(name): given a Unix-style slash path relative to repo root,
returns the internal name for that file (e.g., cls/SourceControl/Git/Utils.cls -> SourceControl.Git.Utils.CLS)
*/
ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1) As %String
ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1, Verbose As %Boolean = 0) As %String
{
set InternalName=""
set Deleted = 0
Expand All @@ -1556,21 +1556,73 @@ ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1) A
if (InternalName="") {
set name=$extract(Name,$length($$$SourceRoot)+1,*)
set name=$replace(name,"\","/") // standardize slash direction
//file is in a subdirectory under the ^Sources root

set nam = name

set queryary=$query($$$SourceMapping(""),-1,dir), mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5)
set queryary=$query($$$SourceMapping(""),-1,dir), mappingsSubscript = $qsubscript(queryary,4)
set subscript=$qsubscript(queryary,5), coverage = $qsubscript(queryary, 6)
set bestMatch = $lb(subscript, coverage, dir)
set bestScore = 0
set currScore = 0
while (queryary'="")&&(mappingsSubscript="mappings") {
if (dir["/")&&(dir=$extract(name, 1, $length(dir))) {
set ext=subscript
set nam = $extract(name, $length(dir)+1, *)
quit
set nam = $extract(name, $length(dir)+1, *)
if ($zconvert(subscript, "U") = $zconvert($piece(name, ".", *), "U")){
set extScore = 1
} else {
set extScore = 0
}

if ((dir["/")&&(dir=$extract(name, 1, $length(dir)))){
set pathScore = 1
} else {
set pathScore = 0
}

if (coverage = "*"){
set covScore = 1
} elseif ($extract($translate(nam, "/", "."), 1, ($length(coverage)+1)) = (coverage_".")) {
set covScore = 2
} else {
set covScore = 0
}

set currScore = (extScore*100) + (pathScore*10) + covScore

if (currScore > bestScore){
set bestScore = currScore
set bestMatch = $lb(subscript, coverage, dir)
} elseif ((currScore = bestScore) && currScore>=111) {
// There are 4 cases here -
// 1. Coverage is more specific in one and Path is the same.
// 2. Path is more specific in one and Coverage is the same.
// 3. Coverage is more specific in one while Path is more specific in the other.
// 4. Both are more specific in one.
// Coverage has higher priority.
// Note that a file extension has no notion of specificity.
// Specificity, for both Coverage and Path, is defined as being directly proportional to the length of the string.

set covSpecific = 0, pathSpecific = 0

if ($length(coverage) > $length($listget(bestMatch, 2))){
set bestMatch = $lb(subscript, coverage, dir)
} elseif ($length(coverage) = $length($listget(bestMatch, 2))){
if ($length(dir) > $length($listget(bestMatch, 3))){
set bestMatch = $lb(subscript, coverage, dir)
}
}
}

set queryary=$query(@queryary,-1,dir)
if (queryary="") {
quit
}
set mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5)
set mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5), coverage = $qsubscript(queryary, 6)
}

if (bestScore >= 111){
set ext = $listget(bestMatch,1)
set dir = $listget(bestMatch, 3)
set nam = $extract(name, $length(dir)+1, *)
}

if ($get(ext)="/CSP/") {
Expand All @@ -1595,6 +1647,17 @@ ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1) A
}
}
if $data(ext)=0 {
if (Verbose){
if (bestScore#100 = 0){
write "No mapping with a matching coverage found for file "_name, !
}
if (((bestScore\10)#10) = 0){
write "No mapping with a matching path found for file "_name, !
}
if (((bestScore\100)#10) = 0){
write "No mapping with a matching extension found for file "_name, !
}
}
quit ""
}
set fileExt=$zconvert(ext,"L")
Expand Down
7 changes: 6 additions & 1 deletion test/UnitTest/SourceControl/Git/NameToInternalNameTest.cls
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ Method TestRegularClassNames()
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls"),"")
// Regular class that doesn't exist and we don't ignore non-existent classes
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls", 1, 0),"SourceControl.Git.DoesNotExist.CLS")
do $$$AssertEquals(##class(Utils).NameToInternalName("test\UnitTest\Git\DoesNotExist.cls", 1, 0),"UnitTest.Git.DoesNotExist.CLS")
do $$$AssertEquals(##class(Utils).NameToInternalName("foo\UnitTest\Foo\Git\DoesNotExist.cls", 1, 0),"UnitTest.Foo.Git.DoesNotExist.CLS")
do $$$AssertEquals(##class(Utils).NameToInternalName("foo\UnitTest\Foo\Git\DoesNotExist.foo", 1, 0),"UnitTest.Foo.Git.DoesNotExist.FOO")
isc-tleavitt marked this conversation as resolved.
Show resolved Hide resolved
}

Method TestPercentClassNames()
Expand Down Expand Up @@ -61,6 +64,9 @@ Method OnBeforeAllTests() As %Status
merge ..Mappings = @##class(SourceControl.Git.Utils).MappingsNode()
kill @##class(SourceControl.Git.Utils).MappingsNode()
set $$$SourceMapping("CLS", "*") = "cls/"
set $$$SourceMapping("CLS", "UnitTest") = "test/"
set $$$SourceMapping("CLS", "UnitTest.Foo") = "foo/"
set $$$SourceMapping("FOO", "*") = "foo/"
set $$$SourceMapping("DFI", "*", "NoFolders") = 1
set $$$SourceMapping("DFI", "*") = "test/_resources/dfi/"
quit $$$OK
Expand All @@ -77,4 +83,3 @@ Method %OnClose() As %Status
}

}