From 47f1bdfa7b2502d44e462766d218b36df0b44c3e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Kl=C3=B6tzke?= Date: Sat, 10 Feb 2024 22:42:36 +0100 Subject: [PATCH] test: add url SCM extraction tests --- test/unit/data/url-scm/foo-1.2.3.tar | Bin 0 -> 10240 bytes test/unit/data/url-scm/foo-1.2.3.tar.bz2 | Bin 0 -> 321 bytes test/unit/data/url-scm/foo-1.2.3.tar.gz | Bin 0 -> 299 bytes test/unit/data/url-scm/foo-1.2.3.tar.xz | Bin 0 -> 332 bytes test/unit/data/url-scm/foo-1.2.3.zip | Bin 0 -> 755 bytes test/unit/data/url-scm/test.txt.gz | Bin 0 -> 33 bytes test/unit/data/url-scm/test.txt.xz | Bin 0 -> 60 bytes test/unit/test_input_urlscm.py | 125 +++++++++++++++++++---- 8 files changed, 103 insertions(+), 22 deletions(-) create mode 100644 test/unit/data/url-scm/foo-1.2.3.tar create mode 100644 test/unit/data/url-scm/foo-1.2.3.tar.bz2 create mode 100644 test/unit/data/url-scm/foo-1.2.3.tar.gz create mode 100644 test/unit/data/url-scm/foo-1.2.3.tar.xz create mode 100644 test/unit/data/url-scm/foo-1.2.3.zip create mode 100644 test/unit/data/url-scm/test.txt.gz create mode 100644 test/unit/data/url-scm/test.txt.xz diff --git a/test/unit/data/url-scm/foo-1.2.3.tar b/test/unit/data/url-scm/foo-1.2.3.tar new file mode 100644 index 0000000000000000000000000000000000000000..b24eac08d99fb70c3307337d5543333d5c25c784 GIT binary patch literal 10240 zcmeIzO;5rw90u@S>8B`gfrDF1J9@x)_Ct8Vy|Bt`O}A;H@w?kyWEv1cfQ0z;vSsbw zeoy;v;oOagW|54^*%}W`CkY7$p%dOec3oW~LM3xCQ(PvRa3RBZ!jiLnQSZTR-7Gz0 zey}thGzW)B$D>^T}^G}^E%&PUf0FM8uF5o}&pUGeOE3GwQ{P{vIjg9M)(mX94b8C9I9Y$Wgz-Il|K0VVOuy_6U>%UZ8{hxyG_j2(_vIT!#FWmZ<-yj$!Nb#>S>qo8|1bZZhMd@{StYux=tuUY*IN z^n(swe7IIjM)!GDIrip!l?@;4Xu6;SpEs?yj4$XXHjb^rxCj9VKmY;|fB*y_009U< d00Izz00bZa0SG_<0uX=z1Rwwb2teR60$)swfc5|Y literal 0 HcmV?d00001 diff --git a/test/unit/data/url-scm/foo-1.2.3.tar.bz2 b/test/unit/data/url-scm/foo-1.2.3.tar.bz2 new file mode 100644 index 0000000000000000000000000000000000000000..a853e3e7e4e82584c60798cf595049a3acaffb5e GIT binary patch literal 321 zcmV-H0lxl1T4*^jL0KkKSuPT1ng9U|e}%@d2mp9v|BV1f5d?q#-UC~wk5BGY8VyfYD&7@&`4C6LJvV<(?FY~uFE_J+LZ9tQko=2W z68JuD$&W5#@~`25qnAEmk41ESaT{=Hs!~{w+F-T|?l8Pa)rGa#f%Zx)64L^*xkJr{ TfsDiq5901frwS4U!c6l(!EuEk literal 0 HcmV?d00001 diff --git a/test/unit/data/url-scm/foo-1.2.3.tar.gz b/test/unit/data/url-scm/foo-1.2.3.tar.gz new file mode 100644 index 0000000000000000000000000000000000000000..95f44ff0018e79ccc8e86190d2282031f206634f GIT binary patch literal 299 zcmV+`0o48o*;!!sGBtQ7) z`LCD3wKhRr5Teq;Qd^Q!j|4Ls{{Pyg6P*FS>VF~o{*SvFK>yF+YyNk8ZTxiXXY>Kb zQXb5ob2;SC#9RLh^#3f@+N4#RDSBQvnYMm$L9}UT{~>H1tj@ONnJn&IcP-znE>%@o x`evv1>{>Aw`WLQU_>->C zOnMdfc!u-Gi3#tWdc%TEsQ!u^=tt}NK?hp{Dx8ltb|#y#GmW|A4&gcKUeD^+w78CA z*L~qV>C1qn=8Q~m`5i0U-e(6YD9ILx-Kn2UH(P2?DxB`dnB=My;Vq1LX|ns_Vl#&* zRbl|tKkbCC>+et;&06|X8K1Ec&>|^z@~GN3ob&$f7GEcVA(E7v+ASyB%h6gxsvlVy z1ei#pT|w0Y%i4+Hw)Xvpf5joq!p0WSq+OHQVYvmryQzD;ubIk@&KmCf-s0 z_Zg&e;x6c(Qt`%DvaOeTNmCHL1In%hhivM#LK=h~nLmErfLyXwYImRj0002ujubvE e=doY_0j2_gPyhhM?KStY#Ao{g000001X)^3?4FJQ literal 0 HcmV?d00001 diff --git a/test/unit/data/url-scm/foo-1.2.3.zip b/test/unit/data/url-scm/foo-1.2.3.zip new file mode 100644 index 0000000000000000000000000000000000000000..73999f7e6a9da41eff4c56c5f8092512a052278f GIT binary patch literal 755 zcmWIWW@h1H00Dsw9uZ&$l;C2JVMxo**EQ5L(lgc%4dG;9e(!KJ)x_~=YH0;G10%}| zW(Ec@5rASwT(p6`FwhJLMm8fkKQApay|gG5ZrCrNfoO&)E9xg@=IIw_aB-z3XXGp7 zXLBLUXG3!%ABr1`i;^MkyaP1$Bc{;^b1K$*MBJB5Smy(DCkTsRn3J2BnWvWwcIaWC zNhdK)Q_jpw&M8evRj?^8Ny*ID%dq3h%qvjC<2P5X()N5=H%on zl;;=aq$tMZDQQ}BaTcYPlosVF7+7=F0)5EHB*%;^9weYKBEaz05yV7}6;?>Bpamkj zd8ly$F^>^w(%VKC6!VZ`h_Jz!k%8>cB`5|%q6TOtC~ELH7BgZHj$P9D2E$BPGyzQp XMH7a}tZYCn3@ktx4)kvu$j=M_q^21Q0O1_p)_{ill=8JN@Z^SK!QUtbetU^wR!1EUN}zRsM3 Q^4mVwG6K~wutY`y07|YBvj6}9 literal 0 HcmV?d00001 diff --git a/test/unit/test_input_urlscm.py b/test/unit/test_input_urlscm.py index 19138838e..96d1867c1 100644 --- a/test/unit/test_input_urlscm.py +++ b/test/unit/test_input_urlscm.py @@ -19,7 +19,7 @@ from bob.input import UrlScm from bob.scm.url import parseMode, dumpMode from bob.invoker import Invoker, InvocationError -from bob.errors import ParseError +from bob.errors import ParseError, BuildError from bob.utils import asHexStr, runInEventLoop, getProcessPoolExecutor, isWindows INVALID_FILE = "C:\\does\\not\\exist" if sys.platform == "win32" else "/does/not/exist/" @@ -43,7 +43,34 @@ class DummyStep: def getPackage(self): return DummyPackage() -class UrlScmTest: +class UrlScmExecutor: + + def invokeScm(self, workspace, scm, switch=False, oldScm=None): + executor = getProcessPoolExecutor() + try: + spec = MagicMock(workspaceWorkspacePath=workspace, envWhiteList=set()) + invoker = Invoker(spec, False, True, True, True, True, False, + executor=executor) + if switch: + runInEventLoop(scm.switch(invoker, oldScm)) + else: + runInEventLoop(scm.invoke(invoker)) + finally: + executor.shutdown() + + def createUrlScm(self, spec = {}, preMirrors=[], fallbackMirrors=[], + defaultFileMode=None): + s = { + 'scm' : 'url', + 'url' : self.url, + 'recipe' : "foo.yaml#0", + '__source' : "Recipe foo", + } + s.update(spec) + return UrlScm(s, preMirrors=preMirrors, fallbackMirrors=fallbackMirrors, + defaultFileMode=defaultFileMode) + +class UrlScmTest(UrlScmExecutor): @classmethod def setUpClass(cls): @@ -75,26 +102,6 @@ def setUpClass(cls): def tearDownClass(cls): cls.__repodir.cleanup() - def invokeScm(self, workspace, scm): - executor = getProcessPoolExecutor() - try: - spec = MagicMock(workspaceWorkspacePath=workspace, envWhiteList=set()) - invoker = Invoker(spec, False, True, True, True, True, False, - executor=executor) - runInEventLoop(scm.invoke(invoker)) - finally: - executor.shutdown() - - def createUrlScm(self, spec = {}, preMirrors=[], fallbackMirrors=[]): - s = { - 'scm' : 'url', - 'url' : self.url, - 'recipe' : "foo.yaml#0", - '__source' : "Recipe foo", - } - s.update(spec) - return UrlScm(s, preMirrors=preMirrors, fallbackMirrors=fallbackMirrors) - def assertContent(self, fn): with open(fn, "rb") as f: d = hashlib.sha1() @@ -808,3 +815,77 @@ def testDump(self): self.assertEqual(dumpMode(0o600), "u=rw") self.assertEqual(dumpMode(0o400), "u=r") self.assertEqual(dumpMode(0o755), "u=rwx,g=rx,o=rx") + + +class TestExtraction(UrlScmExecutor, TestCase): + + def assertFileMd5(self, fn, digest): + with open(fn, "rb") as f: + d = hashlib.md5() + d.update(f.read()) + self.assertEqual(digest, asHexStr(d.digest())) + + def assertTree(self, workspace, prefix="foo-1.2.3"): + self.assertFileMd5(os.path.join(workspace, prefix, "configure"), + "394cded5228db39cb4d040e866134252") + self.assertFileMd5(os.path.join(workspace, prefix, "src/main.c"), + "0790c8c6a3871d0c3bed428b4ae240c4") + + def testArchive(self): + for ext in ("tar", "tar.bz2", "tar.gz", "tar.xz", "zip"): + with self.subTest(extension=ext): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3." + ext)) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm() + self.invokeScm(workspace, scm) + self.assertTree(workspace) + + def testStripComponents(self): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3.tar")) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm({ "stripComponents" : 1 }) + self.invokeScm(workspace, scm) + self.assertTree(workspace, "") + + def testStripComponentsUnsupported(self): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3.zip")) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm({ "stripComponents" : 1 }) + with self.assertRaises(BuildError): + self.invokeScm(workspace, scm) + + @skipIf(isWindows(), "requires UNIX platform") + def testSingleFile(self): + for ext in ("gz", "xz"): + with self.subTest(extension=ext): + self.url = makeFileUrl(os.path.abspath("data/url-scm/test.txt." + ext)) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm() + self.invokeScm(workspace, scm) + self.assertTrue(os.path.exists(os.path.join(workspace, "test.txt." + ext))) + self.assertFileMd5(os.path.join(workspace, "test.txt"), + "d3b07384d113edec49eaa6238ad5ff00") + + def testNoExtrace(self): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3.tar")) + for extract in ("no", False): + with self.subTest(mode=extract): + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm({ "extract" : extract }) + self.invokeScm(workspace, scm) + self.assertTrue(os.path.exists(os.path.join(workspace, "foo-1.2.3.tar"))) + self.assertFalse(os.path.isdir(os.path.join(workspace, "foo-1.2.3"))) + + def testSpecificExtract(self): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3.tar")) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm({ "extract" : "tar" }) + self.invokeScm(workspace, scm) + self.assertTree(workspace) + + def testWrongSpecificExtract(self): + self.url = makeFileUrl(os.path.abspath("data/url-scm/foo-1.2.3.tar")) + with tempfile.TemporaryDirectory() as workspace: + scm = self.createUrlScm({ "extract" : "zip" }) + with self.assertRaises(InvocationError): + self.invokeScm(workspace, scm)