Skip to content

Commit 31c652e

Browse files
authored
Changed the way we match mappings in NameToInternalName, modified and added new testcases (#199)
* Changed the way we match mappings in NameToInternalName, modified and added tests. * Verbose output when getting the InternalName in AddToSourceControl * Add more negative test cases to the unit test Co-authored-by: Sarmishta Velury <[email protected]>
1 parent 6027043 commit 31c652e

File tree

2 files changed

+114
-25
lines changed

2 files changed

+114
-25
lines changed

cls/SourceControl/Git/Utils.cls

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -451,7 +451,7 @@ ClassMethod AddToSourceControl(InternalName As %String) As %Status
451451
set ec = $$$ADDSC(ec, sc)
452452
}
453453
for i=1:1:filenames{
454-
set FileInternalName = ##class(SourceControl.Git.Utils).NormalizeExtension(##class(SourceControl.Git.Utils).NameToInternalName(filenames(i), 0))
454+
set FileInternalName = ##class(SourceControl.Git.Utils).NormalizeExtension(##class(SourceControl.Git.Utils).NameToInternalName(filenames(i), 0,,1))
455455
set FileType = ##class(SourceControl.Git.Utils).Type(FileInternalName)
456456

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

1562-
set queryary=$query($$$SourceMapping(""),-1,dir), mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5)
1562+
set queryary=$query($$$SourceMapping(""),-1,dir), mappingsSubscript = $qsubscript(queryary,4)
1563+
set subscript=$qsubscript(queryary,5), coverage = $qsubscript(queryary, 6)
1564+
set bestMatch = $lb(subscript, coverage, dir)
1565+
set bestScore = 0
1566+
set currScore = 0
15631567
while (queryary'="")&&(mappingsSubscript="mappings") {
1564-
if (dir["/")&&(dir=$extract(name, 1, $length(dir))) {
1565-
set ext=subscript
1566-
set nam = $extract(name, $length(dir)+1, *)
1567-
quit
1568+
set nam = $extract(name, $length(dir)+1, *)
1569+
if ($zconvert(subscript, "U") = $zconvert($piece(name, ".", *), "U")){
1570+
set extScore = 1
1571+
} else {
1572+
set extScore = 0
1573+
}
1574+
1575+
if ((dir["/")&&(dir=$extract(name, 1, $length(dir)))){
1576+
set pathScore = 1
1577+
} else {
1578+
set pathScore = 0
1579+
}
1580+
1581+
if (coverage = "*"){
1582+
set covScore = 1
1583+
} elseif ($extract($translate(nam, "/", "."), 1, ($length(coverage)+1)) = (coverage_".")) {
1584+
set covScore = 2
1585+
} else {
1586+
set covScore = 0
1587+
}
1588+
1589+
set currScore = (extScore*100) + (pathScore*10) + covScore
1590+
1591+
if (currScore > bestScore){
1592+
set bestScore = currScore
1593+
set bestMatch = $lb(subscript, coverage, dir)
1594+
} elseif ((currScore = bestScore) && currScore>=111) {
1595+
// There are 4 cases here -
1596+
// 1. Coverage is more specific in one and Path is the same.
1597+
// 2. Path is more specific in one and Coverage is the same.
1598+
// 3. Coverage is more specific in one while Path is more specific in the other.
1599+
// 4. Both are more specific in one.
1600+
// Coverage has higher priority.
1601+
// Note that a file extension has no notion of specificity.
1602+
// Specificity, for both Coverage and Path, is defined as being directly proportional to the length of the string.
1603+
1604+
set covSpecific = 0, pathSpecific = 0
1605+
1606+
if ($length(coverage) > $length($listget(bestMatch, 2))){
1607+
set bestMatch = $lb(subscript, coverage, dir)
1608+
} elseif ($length(coverage) = $length($listget(bestMatch, 2))){
1609+
if ($length(dir) > $length($listget(bestMatch, 3))){
1610+
set bestMatch = $lb(subscript, coverage, dir)
1611+
}
1612+
}
15681613
}
1614+
15691615
set queryary=$query(@queryary,-1,dir)
15701616
if (queryary="") {
15711617
quit
15721618
}
1573-
set mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5)
1619+
set mappingsSubscript = $qsubscript(queryary,4), subscript=$qsubscript(queryary,5), coverage = $qsubscript(queryary, 6)
1620+
}
1621+
1622+
if (bestScore >= 111){
1623+
set ext = $listget(bestMatch,1)
1624+
set dir = $listget(bestMatch, 3)
1625+
set nam = $extract(name, $length(dir)+1, *)
15741626
}
15751627

15761628
if ($get(ext)="/CSP/") {
@@ -1595,6 +1647,18 @@ ClassMethod NameToInternalName(Name, IgnorePercent = 1, IgnoreNonexistent = 1) A
15951647
}
15961648
}
15971649
if $data(ext)=0 {
1650+
if (Verbose){
1651+
write !
1652+
if (bestScore#100 = 0){
1653+
write !, "No mapping with a matching coverage found for file "_name
1654+
}
1655+
if (((bestScore\10)#10) = 0){
1656+
write !, "No mapping with a matching path found for file "_name
1657+
}
1658+
if ((bestScore\100) = 0){
1659+
write !, "No mapping with a matching extension found for file "_name
1660+
}
1661+
}
15981662
quit ""
15991663
}
16001664
set fileExt=$zconvert(ext,"L")

test/UnitTest/SourceControl/Git/NameToInternalNameTest.cls

Lines changed: 41 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,44 +12,66 @@ Property OldNamespaceTemp As %String;
1212
Method TestRegularClassNames()
1313
{
1414
// Regular class that exists
15-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\SourceControl\Git\Utils.cls"),"SourceControl.Git.Utils.CLS")
15+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\SourceControl\Git\Utils.cls"),"SourceControl.Git.Utils.CLS")
1616
// Regular class that doesn't exist and we ignore non-existent classes
17-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls"),"")
17+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls"),"")
1818
// Regular class that doesn't exist and we don't ignore non-existent classes
19-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls", 1, 0),"SourceControl.Git.DoesNotExist.CLS")
19+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\SourceControl\Git\DoesNotExist.cls", 1, 0),"SourceControl.Git.DoesNotExist.CLS")
20+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("test\UnitTest\Git\DoesNotExist.cls", 1, 0),"UnitTest.Git.DoesNotExist.CLS")
21+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("foo\UnitTest\Foo\Git\DoesNotExist.cls", 1, 0),"UnitTest.Foo.Git.DoesNotExist.CLS")
22+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("foo\UnitTest\Foo\Git\DoesNotExist.foo", 1, 0),"UnitTest.Foo.Git.DoesNotExist.FOO")
2023
}
2124

2225
Method TestPercentClassNames()
2326
{
2427
// % class that exists but we ignore % classes
25-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\Base.cls"),"")
28+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\Base.cls"),"")
2629
// % class that exists and we don't ignore % classes
27-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\Base.cls", 0),"%Studio.Extension.Base.CLS")
30+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\Base.cls", 0),"%Studio.Extension.Base.CLS")
2831
// % class that doesn't exist and we ignore non-existent classes
29-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\DoesNotExist.cls", 0),"")
32+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\DoesNotExist.cls", 0),"")
3033
// % class that doesn't exist and we don't ignore non-existent classes
31-
do $$$AssertEquals(##class(Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\DoesNotExist.cls", 0, 0),"%Studio.Extension.DoesNotExist.CLS")
34+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("cls\"_##class(SourceControl.Git.Utils).PercentClassReplace()_"Studio\Extension\DoesNotExist.cls", 0, 0),"%Studio.Extension.DoesNotExist.CLS")
3235
}
3336

3437
Method TestAbstractDocumentClassNames()
3538
{
3639
// %Studio.AbstractDocument type that exists
3740
do ##class(%RoutineMgr).Delete("test2.pivot.DFI")
38-
do $$$AssertEquals(##class(Utils).NameToInternalName("test\_resources\dfi\test2.pivot.dfi"),"")
39-
do $$$AssertStatusOK(##class(Utils).ImportItem("test2.pivot.DFI",1))
40-
do $$$AssertEquals(##class(Utils).NameToInternalName("test\_resources\dfi\test2.pivot.dfi"),"test2.pivot.DFI")
41+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("test\_resources\dfi\test2.pivot.dfi"),"")
42+
do $$$AssertStatusOK(##class(SourceControl.Git.Utils).ImportItem("test2.pivot.DFI",1))
43+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("test\_resources\dfi\test2.pivot.dfi"),"test2.pivot.DFI")
4144
// %Studio.AbstractDocument type that does not exist and we ignore non-existent classes
42-
do $$$AssertEquals(##class(Utils).NameToInternalName("test\_resources\dfi\DoesNotExist.dfi"),"")
45+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("test\_resources\dfi\DoesNotExist.dfi"),"")
4346
// %Studio.AbstractDocument type that doesn't exist and we don't ignore non-existent classes
44-
do $$$AssertEquals(##class(Utils).NameToInternalName("test\_resources\dfi\DoesNotExist.dfi", 1, 0),"DoesNotExist.DFI")
47+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("test\_resources\dfi\DoesNotExist.dfi", 1, 0),"DoesNotExist.DFI")
4548
}
4649

4750
Method TestStaticFileNames()
4851
{
4952
// Static file that shouldn't be on the server
50-
do $$$AssertEquals(##class(Utils).NameToInternalName("git-webui\src\js\git-webui.js"),"")
51-
// Static file that shouldn't be on the server but we don't ignore non-existent classes
52-
do $$$AssertEquals(##class(Utils).NameToInternalName("git-webui\src\js\git-webui.js", 1, 0),"")
53+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("git-webui\src\js\git-webui.js"),"")
54+
// Static file that shouldn't be on the server but we don't ignore non-existent classes (000 composite score)
55+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("git-webui\src\js\git-webui.js", 1, 0, 1),"")
56+
}
57+
58+
Method TestNegative()
59+
{
60+
// Based on composite scores
61+
62+
// 000 is covered in TestStaticFileNames()
63+
// 001 and 002
64+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("barq\MyBarFile1.barq", 1, 0, 1),"")
65+
// 010
66+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("bar\NotMyBarFile1.barq", 1, 0, 1),"")
67+
// 011 and 012
68+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("bar\MyBarFile1.barq", 1, 0, 1),"")
69+
// 100
70+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("barq\NotMyBarFile1.bar", 1, 0, 1),"")
71+
// 101 and 102
72+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("barq\MyBarFile1.bar", 1, 0, 1),"")
73+
// 110
74+
do $$$AssertEquals(##class(SourceControl.Git.Utils).NameToInternalName("bar\NotMyBarFile1.bar", 1, 0, 1),"")
5375
}
5476

5577
Method OnBeforeAllTests() As %Status
@@ -61,6 +83,10 @@ Method OnBeforeAllTests() As %Status
6183
merge ..Mappings = @##class(SourceControl.Git.Utils).MappingsNode()
6284
kill @##class(SourceControl.Git.Utils).MappingsNode()
6385
set $$$SourceMapping("CLS", "*") = "cls/"
86+
set $$$SourceMapping("CLS", "UnitTest") = "test/"
87+
set $$$SourceMapping("CLS", "UnitTest.Foo") = "foo/"
88+
set $$$SourceMapping("FOO", "*") = "foo/"
89+
set $$$SourceMapping("BAR", "MyBarFile") = "bar/"
6490
set $$$SourceMapping("DFI", "*", "NoFolders") = 1
6591
set $$$SourceMapping("DFI", "*") = "test/_resources/dfi/"
6692
quit $$$OK
@@ -77,4 +103,3 @@ Method %OnClose() As %Status
77103
}
78104

79105
}
80-

0 commit comments

Comments
 (0)