Skip to content

Commit c48af63

Browse files
authored
Merge pull request #827 from intersystems/test-pull
Integration tests for pull and sync
2 parents cbcdc5b + 392fcc3 commit c48af63

File tree

3 files changed

+133
-0
lines changed

3 files changed

+133
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Class UnitTest.SourceControl.Git.AbstractTest Extends %UnitTest.TestCase
2+
{
3+
4+
Property InitialExtension As %String [ InitialExpression = {##class(%Studio.SourceControl.Interface).SourceControlClassGet()} ];
5+
6+
Property SourceControlGlobal [ MultiDimensional ];
7+
8+
Method %OnNew(initvalue) As %Status
9+
{
10+
Merge ..SourceControlGlobal = ^SYS("SourceControl")
11+
Kill ^SYS("SourceControl")
12+
Set settings = ##class(SourceControl.Git.Settings).%New()
13+
Set settings.namespaceTemp = ##class(%Library.File).TempFilename()_"dir"
14+
Set settings.Mappings("CLS","*")="cls/"
15+
Do settings.%Save()
16+
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet("SourceControl.Git.Extension")
17+
Quit ##super(initvalue)
18+
}
19+
20+
Method %OnClose() As %Status [ Private, ServerOnly = 1 ]
21+
{
22+
Do ##class(%Studio.SourceControl.Interface).SourceControlClassSet(..InitialExtension)
23+
Kill ^SYS("SourceControl")
24+
Merge ^SYS("SourceControl") = ..SourceControlGlobal
25+
Quit $$$OK
26+
}
27+
28+
ClassMethod WriteFile(filePath, contents)
29+
{
30+
set dirPath = ##class(%File).GetDirectory(filePath)
31+
if '##class(%File).CreateDirectoryChain(dirPath,.ret) {
32+
$$$ThrowStatus($$$ERROR($$$GeneralError,"failed to create directory: "_ret))
33+
}
34+
set fileStream = ##class(%Stream.FileCharacter).%OpenId(filePath,,.sc)
35+
$$$ThrowOnError(sc)
36+
do fileStream.Write(contents)
37+
$$$ThrowOnError(fileStream.%Save())
38+
}
39+
40+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Class UnitTest.SourceControl.Git.Pull Extends UnitTest.SourceControl.Git.AbstractTest
2+
{
3+
4+
Method TestPull()
5+
{
6+
// initialize remote repository on filesystem
7+
set remoteDir = ##class(%Library.File).TempFilename()_"d"
8+
if '##class(%File).CreateDirectoryChain(remoteDir_"/cls",.ret) {
9+
$$$ThrowStatus($$$ERROR($$$GeneralError,"failed to create directory: "_ret))
10+
}
11+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass1.cls","Class TestGit.SampleClass1 {}")
12+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass2.cls","Class TestGit.SampleClass2 {}")
13+
do $zf(-100,"/SHELL","git","init",remoteDir)
14+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "config", "user.email", "[email protected]")
15+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "config", "user.name", "Unit Test")
16+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "add", ".")
17+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "commit", "-m", "initial commit in remote for unit test")
18+
// initialize local repo, cloning remote.
19+
$$$ThrowOnError(##class(SourceControl.Git.Utils).Clone(remoteDir_"/.git"))
20+
// import all and confirm classes exist
21+
do $System.OBJ.Delete("TestGit.SampleClass1,TestGit.SampleClass2")
22+
$$$ThrowOnError(##class(SourceControl.Git.Utils).ImportAll(1))
23+
do $$$AssertTrue($$$comClassDefined("TestGit.SampleClass1"))
24+
do $$$AssertTrue($$$comClassDefined("TestGit.SampleClass2"))
25+
// delete, add, and modify classes on remote. add and commit them all on remote.
26+
if '##class(%File).Delete(remoteDir_"/cls/TestGit/SampleClass1.cls",.ret) {
27+
$$$ThrowStatus($$$ERROR($$$GeneralError,"failed to delete class file"))
28+
}
29+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass2.cls","Class TestGit.SampleClass2 { Parameter foo = ""bar""; }")
30+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass3.cls","Class TestGit.SampleClass3 {}")
31+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "add", ".")
32+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "commit", "-m", "delete, modify, and add classes on remote")
33+
// pull on local and confirm changes were loaded.
34+
$$$ThrowOnError(##class(SourceControl.Git.API).Pull())
35+
do $$$AssertNotTrue($$$comClassDefined("TestGit.SampleClass1"))
36+
do $$$AssertEquals(##class(TestGit.SampleClass2).#foo, "bar")
37+
do $$$AssertTrue($$$comClassDefined("TestGit.SampleClass3"))
38+
}
39+
40+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
Class UnitTest.SourceControl.Git.Sync Extends UnitTest.SourceControl.Git.AbstractTest
2+
{
3+
4+
Method TestSync()
5+
{
6+
do $$$LogMessage("set up remote repo on filesystem")
7+
set remoteDir = ##class(%Library.File).TempFilename()_"d"
8+
if '##class(%File).CreateDirectoryChain(remoteDir,.ret) {
9+
$$$ThrowStatus($$$ERROR($$$GeneralError,"failed to create directory: "_ret))
10+
}
11+
do $zf(-100,"/SHELL","git","init",remoteDir)
12+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "config", "user.email", "[email protected]")
13+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "config", "user.name", "Unit Test")
14+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "checkout", "-b", "live")
15+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass1.cls","Class TestGit.SampleClass1 {}")
16+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "add", ".")
17+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "commit", "-m", "initial commit in remote for unit test")
18+
do $$$LogMessage("initialize local repo cloning remote")
19+
$$$ThrowOnError(##class(SourceControl.Git.Utils).Clone(remoteDir_"/.git"))
20+
do $$$LogMessage("set default merge branch to live and enable basic mode")
21+
set settings = ##class(SourceControl.Git.Settings).%New()
22+
set settings.defaultMergeBranch = "live"
23+
set settings.basicMode = "system"
24+
set settings.systemBasicMode = 1
25+
$$$ThrowOnError(settings.%Save())
26+
do $$$LogMessage("check out live branch on local")
27+
$$$ThrowOnError(##class(SourceControl.Git.Utils).SwitchBranch("live"))
28+
do $$$LogMessage("create a class through IRIS, add it to source control, and sync")
29+
do $System.OBJ.Delete("TestGit.SampleClass2")
30+
set classDef = ##class(%Dictionary.ClassDefinition).%New("TestGit.SampleClass2")
31+
$$$ThrowOnError(classDef.%Save())
32+
$$$ThrowOnError($System.OBJ.Compile("TestGit.SampleClass2"))
33+
$$$ThrowOnError(##class(SourceControl.Git.Utils).AddToSourceControl("TestGit.SampleClass2.cls"))
34+
$$$ThrowOnError(##class(SourceControl.Git.Utils).Sync("should not commit"))
35+
do $$$LogMessage("sync should NOT have committed the new file since we are on the live branch.")
36+
do $$$AssertTrue(##class(SourceControl.Git.Change).IsUncommitted(##class(SourceControl.Git.Utils).FullExternalName("TestGit.SampleClass2.cls")))
37+
do $$$LogMessage("now, check out an interface branch")
38+
$$$ThrowOnError(##class(SourceControl.Git.Utils).NewBranch("interface"))
39+
do $$$LogMessage("simulate another developer's change going live")
40+
do ..WriteFile(remoteDir_"/cls/TestGit/SampleClass1.cls","Class TestGit.SampleClass1 { Parameter foo = ""bar""; }")
41+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "add", ".")
42+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "commit", "-m", "initial commit in remote for unit test")
43+
do $$$LogMessage("check out an interface branch and sync")
44+
$$$ThrowOnError(##class(SourceControl.Git.Utils).Sync("should commit"))
45+
do $$$LogMessage("sync should have rebased the other developer's change, and committed the new file.")
46+
do $$$AssertEquals(##class(TestGit.SampleClass1).#foo, "bar")
47+
do $$$AssertNotTrue(##class(SourceControl.Git.Change).IsUncommitted(##class(SourceControl.Git.Utils).FullExternalName("TestGit.SampleClass2.cls")))
48+
do $$$LogMessage("simulate a merge request on the remote from the interface branch to live.")
49+
do $zf(-100,"/SHELL","git", "-C", remoteDir, "merge", "interface")
50+
do $$$AssertTrue(##class(%File).Exists(remoteDir_"/cls/TestGit/SampleClass2.cls"))
51+
}
52+
53+
}

0 commit comments

Comments
 (0)