How to use assertFileEqual method in autotest

Best Python code snippet using autotest_python

test_full.py

Source:test_full.py Github

copy

Full Screen

...30 def tearDown(self):31 ''' clean up the patch test temporary directory and return to the old cwd '''32 os.chdir(self.old_cwd)33 self.tmp_dir.cleanup()34 def assertFileEqual(self, path, content):35 ''' assert that the given path has the expected file content '''36 with open(path, 'r', encoding='utf8') as file:37 file_content = file.read()38 self.assertEqual(file_content, content)39 def assertFileBinaryEqual(self, path, content):40 ''' assert that the given path has the expected file content '''41 with open(path, 'rb') as file:42 file_content = file.read()43 self.assertEqual(file_content, content)44 def test_basic_patch(self):45 ''' the most basic of patch tests - hopefully should never fail! '''46 patch = '''47--- to_patch 2022-06-19 16:56:12.974516527 +120048+++ to_patch 2022-06-19 16:56:24.666877199 +120049@@ -1,3 +1,4 @@50 int main()51 {52+ return 0;53 }54'''55 with open('diff.patch', 'w', encoding='utf8') as patch_file:56 patch_file.write(patch)57 to_patch = '''int main()58{59}60'''61 with open('to_patch', 'w', encoding='utf8') as to_patch_file:62 to_patch_file.write(to_patch)63 ret = run_patch('patch -i diff.patch')64 self.assertEqual(ret.returncode, 0)65 self.assertEqual(ret.stdout, 'patching file to_patch\n')66 self.assertEqual(ret.stderr, '')67 self.assertFileEqual('to_patch', '''int main()68{69 return 0;70}71''')72 def test_basic_unicode_patch(self):73 ''' test a basic patch which has a non ASCII filename '''74 patch = '''75--- to_patch 2022-06-19 16:56:12.974516527 +120076+++ to_patch 2022-06-19 16:56:24.666877199 +120077@@ -1,3 +1,4 @@78 int main()79 {80+ return 0;81 }82'''83 with open('ハローワールド.patch', 'w', encoding='utf8') as patch_file:84 patch_file.write(patch)85 to_patch = '''int main()86{87}88'''89 with open('to_patch', 'w', encoding='utf8') as to_patch_file:90 to_patch_file.write(to_patch)91 ret = run_patch('patch -i ハローワールド.patch')92 self.assertEqual(ret.stderr, '')93 self.assertEqual(ret.stdout, 'patching file to_patch\n')94 self.assertEqual(ret.returncode, 0)95 self.assertFileEqual('to_patch', '''int main()96{97 return 0;98}99''')100 def test_basic_unicode_patch_filepaths(self):101 ''' test a patch which is changing a non ASCII filename '''102 patch = '''103--- "\\327\\251\\327\\234\\327\\225\\327\\235 \\327\\242\\327\\225\\327\\234\\327\\235!" 2022-09-03 14:51:28.429821767 +1200104+++ another 2022-09-03 14:52:15.250024346 +1200105@@ -1,3 +1,2 @@106 a107-b108 c109'''110 with open('diff.patch', 'w', encoding='utf8') as patch_file:111 patch_file.write(patch)112 to_patch = 'a\nb\nc\n'113 with open('שלום עולם!', 'w', encoding='utf8') as to_patch_file:114 to_patch_file.write(to_patch)115 ret = run_patch('patch -i diff.patch')116 self.assertEqual(ret.stderr, '')117 self.assertEqual(ret.stdout, 'patching file שלום עולם!\n')118 self.assertEqual(ret.returncode, 0)119 self.assertFileEqual('שלום עולם!', 'a\nc\n')120 def test_unicode_rename_git_no_quoting(self):121 ''' test a git patch (without quote path) renaming a unicode file '''122 patch = '''123commit f3f4654d6a154907dbd36c47d49c910b0c10c072124Author: Shannon Booth <shannon.ml.booth@gmail.com>125Date: Sun Sep 4 11:03:05 2022 +1200126 Add unicode path using core.quotePath false127diff --git a/file b/지배128similarity index 66%129rename from file130rename to 지배131index de98044..0f7bc76 100644132--- a/նախքան133+++ b/지배134@@ -1,3 +1,2 @@135 Мир136-b137 c138'''139 with open('diff.patch', 'w', encoding='utf8') as patch_file:140 patch_file.write(patch)141 to_patch = 'Мир\nb\nc\n'142 with open('նախքան', 'w', encoding='utf8') as to_patch_file:143 to_patch_file.write(to_patch)144 ret = run_patch('patch -idiff.patch')145 self.assertEqual(ret.stderr, '')146 self.assertEqual(ret.stdout, 'patching file 지배 (renamed from նախքան)\n')147 self.assertEqual(ret.returncode, 0)148 self.assertFileEqual('지배', 'Мир\nc\n')149 def test_set_patch_file(self):150 ''' test that setting file to patch works as expected '''151 patch = '''152--- a 2022-06-19 16:56:12.974516527 +1200153+++ b 2022-06-19 16:56:24.666877199 +1200154@@ -1,3 +1,4 @@155 int main()156 {157+ return 0;158 }159'''160 with open('diff.patch', 'w', encoding='utf8') as patch_file:161 patch_file.write(patch)162 to_patch = '''int main()163{164}165'''166 with open('c', 'w', encoding='utf8') as to_patch_file:167 to_patch_file.write(to_patch)168 ret = run_patch('patch -i diff.patch c')169 self.assertEqual(ret.returncode, 0)170 self.assertEqual(ret.stdout, 'patching file c\n')171 self.assertEqual(ret.stderr, '')172 self.assertFileEqual('c', '''int main()173{174 return 0;175}176''')177 def test_basic_context_patch(self):178 ''' the most basic of context patch tests - hopefully should never fail! '''179 patch = '''180*** a 2022-06-19 20:26:52.280538615 +1200181--- b 2022-06-19 20:26:59.968648316 +1200182***************183*** 1,3 ****184--- 1,4 ----185 int main()186 {187+ return 0;188 }189'''190 with open('diff.patch', 'w', encoding='utf8') as patch_file:191 patch_file.write(patch)192 to_patch = '''int main()193{194}195'''196 with open('a', 'w', encoding='utf8') as to_patch_file:197 to_patch_file.write(to_patch)198 ret = run_patch('patch -i diff.patch')199 self.assertEqual(ret.stdout, 'patching file a\n')200 self.assertEqual(ret.stderr, '')201 self.assertEqual(ret.returncode, 0)202 self.assertFileEqual('a', '''int main()203{204 return 0;205}206''')207 def test_normal_patch_with_trailing_newline(self):208 ''' test that a normal patch with a trailing newline applies without errors '''209 patch = '''2d1210< 2211'''212 with open('diff.patch', 'w', encoding='utf8') as patch_file:213 patch_file.write(patch)214 to_patch = '1\n2\n3\n'215 with open('a', 'w', encoding='utf8') as to_patch_file:216 to_patch_file.write(to_patch)217 ret = run_patch('patch -i diff.patch a')218 self.assertEqual(ret.stdout, 'patching file a\n')219 self.assertEqual(ret.stderr, '')220 self.assertEqual(ret.returncode, 0)221 self.assertFileEqual('a', '1\n3\n')222 def test_less_basic_patch(self):223 ''' test a slightly more complex patch '''224 patch = '''225--- x 2022-10-10 20:45:19.577907405 +1300226+++ x 2022-10-10 13:03:25.792099411 +1300227@@ -1,9 +1,8 @@228 1229 2230-3231+3a232 4233 5234 6235-7236 8237 9238'''239 with open('diff.patch', 'w', encoding='utf8') as patch_file:240 patch_file.write(patch)241 to_patch = '''124222433244424552466247724882499250'''251 with open('x', 'w', encoding='utf8') as to_patch_file:252 to_patch_file.write(to_patch)253 ret = run_patch('patch -i diff.patch')254 self.assertEqual(ret.returncode, 0)255 self.assertEqual(ret.stdout, 'patching file x\n')256 self.assertEqual(ret.stderr, '')257 self.assertFileEqual('x', '''125822593a26042615262626382649265''')266 def test_basic_patch_with_dryrun(self):267 ''' basic patch applied with dry run '''268 patch = '''269--- to_patch 2022-06-19 16:56:12.974516527 +1200270+++ to_patch 2022-06-19 16:56:24.666877199 +1200271@@ -1,3 +1,4 @@272 int main()273 {274+ return 0;275 }276'''277 with open('diff.patch', 'w', encoding='utf8') as patch_file:278 patch_file.write(patch)279 to_patch = '''int main()280{281}282'''283 with open('to_patch', 'w', encoding='utf8') as to_patch_file:284 to_patch_file.write(to_patch)285 ret = run_patch('patch -i diff.patch --dry-run')286 self.assertEqual(ret.returncode, 0)287 self.assertEqual(ret.stdout, 'checking file to_patch\n')288 self.assertEqual(ret.stderr, '')289 self.assertFileEqual('to_patch', to_patch)290 self.assertFalse(os.path.exists('to_patch.orig'))291 def test_patch_dash_filename_patch(self):292 ''' test that patch still applies a patch to a file named '-' '''293 patch = '''294diff --git a/- b/-295new file mode 100644296index 0000000..7a1c613297--- /dev/null298+++ b/-299@@ -0,0 +1 @@300+some file301'''302 with open('diff.patch', 'w', encoding='utf8') as patch_file:303 patch_file.write(patch)304 ret = run_patch('patch -i diff.patch')305 self.assertEqual(ret.returncode, 0)306 self.assertEqual(ret.stdout, 'patching file -\n')307 self.assertEqual(ret.stderr, '')308 self.assertFileEqual('-', 'some file\n')309 def test_basic_patch_with_dryrun_to_stdout(self):310 ''' basic patch applied with dry run to stdout '''311 patch = '''312--- to_patch 2022-06-19 16:56:12.974516527 +1200313+++ to_patch 2022-06-19 16:56:24.666877199 +1200314@@ -1,3 +1,4 @@315 int main()316 {317+ return 0;318 }319'''320 with open('diff.patch', 'w', encoding='utf8') as patch_file:321 patch_file.write(patch)322 to_patch = '''int main()323{324}325'''326 with open('to_patch', 'w', encoding='utf8') as to_patch_file:327 to_patch_file.write(to_patch)328 ret = run_patch('patch -i diff.patch --dry-run -o -')329 self.assertEqual(ret.returncode, 0)330 self.assertEqual(ret.stderr, 'checking file - (read from to_patch)\n')331 self.assertEqual(ret.stdout, '''\332int main()333{334 return 0;335}336''')337 self.assertFileEqual('to_patch', to_patch)338 self.assertFalse(os.path.exists('to_patch.orig'))339 def test_failed_patch_dry_run(self):340 ''' test a bad patch with dry run does not write rejects '''341 patch = '''342--- 1 2022-06-26 12:22:22.161398905 +1200343+++ 2 2022-06-26 12:22:44.105278030 +1200344@@ -1,3 +1,2 @@345 1346-2347 3348'''349 with open('diff.patch', 'w', encoding='utf8') as patch_file:350 patch_file.write(patch)351 to_patch = '''a352b353c354'''355 with open('1', 'w', encoding='utf8') as to_patch_file:356 to_patch_file.write(to_patch)357 ret = run_patch('patch -i diff.patch --dry-run --force')358 self.assertEqual(ret.returncode, 1)359 self.assertEqual(ret.stdout, '''checking file 1360Hunk #1 FAILED at 1.3611 out of 1 hunk FAILED362''')363 self.assertEqual(ret.stderr, '')364 self.assertFileEqual('1', to_patch)365 self.assertFalse(os.path.exists('1.rej'))366 self.assertFalse(os.path.exists('1.orig'))367 def test_ed_patches_not_supported(self):368 ''' test that when we try patch an ed patch file, we fail with an appropriate error '''369 patch = '3d\n'370 with open('diff.patch', 'w', encoding='utf8') as patch_file:371 patch_file.write(patch)372 ret = run_patch('patch -i diff.patch --ed')373 self.assertEqual(ret.returncode, 2)374 self.assertEqual(ret.stdout, '')375 self.assertEqual(ret.stderr, 'patch: **** ed format patches are not supported by this version of patch\n')376 def test_add_file(self):377 ''' test that a patch which adds a file actually ends up adding said file '''378 patch = '''379--- /dev/null 2022-05-27 08:55:08.788091961 +1200380+++ add 2022-05-28 17:03:10.882372978 +1200381@@ -0,0 +1,3 @@382+int main()383+{384+}385'''386 with open('diff.patch', 'w', encoding='utf8') as patch_file:387 patch_file.write(patch)388 self.assertFalse(os.path.exists('add')) # sanity check389 ret = run_patch('patch -i diff.patch')390 self.assertEqual(ret.returncode, 0)391 self.assertEqual(ret.stdout, 'patching file add\n')392 self.assertEqual(ret.stderr, '')393 self.assertFileEqual('add', '''int main()394{395}396''')397 def test_add_file_using_basename(self):398 ''' test a patch with default strip '''399 patch = '''400--- /dev/null 2022-05-27 08:55:08.788091961 +1200401+++ a/b/c/d/e 2022-05-28 17:03:10.882372978 +1200402@@ -0,0 +1,3 @@403+int main()404+{405+}406'''407 with open('diff.patch', 'w', encoding='utf8') as patch_file:408 patch_file.write(patch)409 self.assertFalse(os.path.exists('e')) # sanity check410 ret = run_patch('patch -i diff.patch')411 self.assertEqual(ret.returncode, 0)412 self.assertEqual(ret.stdout, 'patching file e\n')413 self.assertEqual(ret.stderr, '')414 self.assertFileEqual('e', '''int main()415{416}417''')418 def test_add_file_missing_folders(self):419 ''' test a patch adding a file with containing folders missing '''420 patch = '''421--- /dev/null 2022-05-27 08:55:08.788091961 +1200422+++ a/b/c/d/e 2022-05-28 17:03:10.882372978 +1200423@@ -0,0 +1,3 @@424+int main()425+{426+}427'''428 with open('diff.patch', 'w', encoding='utf8') as patch_file:429 patch_file.write(patch)430 self.assertFalse(os.path.exists('e')) # sanity check431 ret = run_patch('patch -i diff.patch -p0')432 self.assertEqual(ret.stdout, 'patching file a/b/c/d/e\n')433 self.assertEqual(ret.stderr, '')434 self.assertEqual(ret.returncode, 0)435 self.assertFileEqual('a/b/c/d/e', '''int main()436{437}438''')439 def test_override_reject_file(self):440 ''' applying a rejected patch with overridden reject file writes to correct location '''441 patch = '''\442--- a 2022-07-08 10:34:03.860546761 +1200443+++ b 2022-07-08 10:34:20.096714313 +1200444@@ -1,3 +1,3 @@445-a446-b447-c448+1449+2450+3451'''452 with open('diff.patch', 'w', encoding='utf8') as patch_file:453 patch_file.write(patch)454 to_patch = '1\n2\n3\n'455 with open('a', 'w', encoding='utf8') as to_patch_file:456 to_patch_file.write(to_patch)457 ret = run_patch('patch -i diff.patch --force -r override.rej')458 self.assertEqual(ret.stdout, '''patching file a459Hunk #1 FAILED at 1.4601 out of 1 hunk FAILED -- saving rejects to file override.rej461''')462 self.assertEqual(ret.stderr, '')463 self.assertEqual(ret.returncode, 1)464 self.assertFileEqual('override.rej', patch)465 def test_reject_format_context(self):466 ''' test a reject format of context '''467 patch = '''\468--- a/reject 2022-07-30 11:40:37.280248088 +1200469+++ b/reject 2022-07-30 11:40:49.032177150 +1200470@@ -1,3 +1,3 @@471 abc472-def473+123474 ghi475'''476 with open('diff.patch', 'w', encoding='utf8') as patch_file:477 patch_file.write(patch)478 to_patch = 'apc\nzxy\nghi\n'479 with open('reject', 'w', encoding='utf8') as to_patch_file:480 to_patch_file.write(to_patch)481 ret = run_patch('patch -i diff.patch --reject-format context')482 self.assertEqual(ret.stdout, '''patching file reject483Hunk #1 FAILED at 1.4841 out of 1 hunk FAILED -- saving rejects to file reject.rej485''')486 self.assertEqual(ret.stderr, '')487 self.assertEqual(ret.returncode, 1)488 self.assertFileEqual('reject.rej', '''\489*** reject 2022-07-30 11:40:37.280248088 +1200490--- reject 2022-07-30 11:40:49.032177150 +1200491***************492*** 1,3 ****493 abc494! def495 ghi496--- 1,3 ----497 abc498! 123499 ghi500''')501 def test_remove_file_in_folders(self):502 ''' test a patch removing a file removes all containing empty folders '''503 patch = '''504--- a/b/c/d/e 2022-07-03 14:32:47.081054897 +1200505+++ /dev/null 2022-06-30 20:33:13.470250591 +1200506@@ -1 +0,0 @@507-1508'''509 with open('diff.patch', 'w', encoding='utf8') as patch_file:510 patch_file.write(patch)511 path = os.path.join('a', 'b', 'c', 'd')512 os.makedirs(path)513 to_patch = '1\n'514 with open(os.path.join(path, 'e'), 'w', encoding='utf8') as to_patch_file:515 to_patch_file.write(to_patch)516 with open(os.path.join('a', '1'), 'w', encoding='utf8') as another_file:517 another_file.write('some stuff!\n')518 # Test that the file and empty directory are removed, but the519 # non empty directory is not removed.520 ret = run_patch('patch -i diff.patch -p0')521 self.assertEqual(ret.stdout, 'patching file a/b/c/d/e\n')522 self.assertEqual(ret.stderr, '')523 self.assertEqual(ret.returncode, 0)524 self.assertFalse(os.path.exists(os.path.join('a', 'b', 'c', 'd')))525 self.assertFalse(os.path.exists(os.path.join('a', 'b', 'c')))526 self.assertFalse(os.path.exists(os.path.join('a', 'b')))527 self.assertFileEqual(os.path.join('a', '1'), 'some stuff!\n')528 def test_remove_file_successfully(self):529 ''' test removal patch that has some garbage not found in patch file does not clean file '''530 patch = '''531--- to_remove 2022-06-06 14:40:51.547454892 +1200532+++ /dev/null 2022-06-01 16:08:26.275921370 +1200533@@ -1,3 +0,0 @@534-int main()535-{536-}537'''538 with open('diff.patch', 'w', encoding='utf8') as patch_file:539 patch_file.write(patch)540 to_patch = '''int main()541{542}543'''544 with open('to_remove', 'w', encoding='utf8') as to_patch_file:545 to_patch_file.write(to_patch)546 self.assertTrue(os.path.exists('to_remove'))547 ret = run_patch('patch -i diff.patch')548 self.assertEqual(ret.stdout, 'patching file to_remove\n')549 self.assertEqual(ret.stderr, '')550 self.assertEqual(ret.returncode, 0)551 self.assertFalse(os.path.exists('to_remove'))552 def test_remove_file_that_has_trailing_garbage(self):553 ''' test removal patch that has some garbage not found in patch file does not clean file '''554 patch = '''555--- remove 2022-06-06 14:40:51.547454892 +1200556+++ /dev/null 2022-06-01 16:08:26.275921370 +1200557@@ -1,3 +0,0 @@558-int main()559-{560-}561'''562 with open('diff.patch', 'w', encoding='utf8') as patch_file:563 patch_file.write(patch)564 to_patch = '''int main()565{566}567// some trailing garbage568'''569 with open('remove', 'w', encoding='utf8') as to_patch_file:570 to_patch_file.write(to_patch)571 ret = run_patch('patch -i diff.patch')572 self.assertEqual(ret.stdout, '''patching file remove573Not deleting file remove as content differs from patch574''')575 self.assertEqual(ret.stderr, '')576 self.assertEqual(ret.returncode, 1)577 self.assertFileEqual('remove', '// some trailing garbage\n')578 def test_backup_when_patch_is_adding_file(self):579 ''' test that when a patch creating a file has the backup option, a empty backup is made '''580 patch = '''581--- /dev/null 2022-06-08 13:09:24.217949672 +1200582+++ f 2022-06-08 20:44:55.455275623 +1200583@@ -0,0 +1 @@584+a line585'''586 with open('diff.patch', 'w', encoding='utf8') as patch_file:587 patch_file.write(patch)588 self.assertFalse(os.path.exists('f')) # sanity check589 self.assertFalse(os.path.exists('f.orig')) # sanity check590 ret = run_patch('patch -b -i diff.patch')591 self.assertEqual(ret.stderr, '')592 self.assertEqual(ret.returncode, 0)593 self.assertEqual(ret.stdout, 'patching file f\n')594 self.assertFileEqual('f', 'a line\n')595 self.assertFileEqual('f.orig', '')596 def test_backup_rename_patch(self):597 ''' test that no backup is created for a rename patch '''598 # NOTE: this might very well be a bug. However, GNU patch599 # also has this behavior, so this test is to ensure any600 # change here is made deliberately.601 patch = '''602commit 61593eeba9cf1663927cbccec2a15a987b6f9e53603Author: Shannon Booth <shannon.ml.booth@gmail.com>604Date: Sun Sep 4 11:27:52 2022 +1200605 rename606diff --git a/a b/b607similarity index 100%608rename from a609rename to b610'''611 # FIXME: add a test for no newline patch...612 with open('diff.patch', 'w', encoding='utf8') as patch_file:613 patch_file.write(patch)614 to_patch = 'ab'615 with open('a', 'w', encoding='utf8') as to_patch_file:616 to_patch_file.write(to_patch)617 ret = run_patch('patch -b -i diff.patch')618 self.assertEqual(ret.stderr, '')619 self.assertEqual(ret.returncode, 0)620 self.assertEqual(ret.stdout, 'patching file b (renamed from a)\n')621 self.assertFileEqual('b', to_patch)622 self.assertFileEqual('b.orig', '')623 self.assertEqual(set(os.listdir()), {'b', 'b.orig', 'diff.patch'})624 def test_backup_on_top_of_existing_file(self):625 ''' test that when a patch creating a file has the backup option, a empty backup is made '''626 patch = '''627--- a 2022-06-19 13:12:11.460782360 +1200628+++ b 2022-06-19 13:12:19.472676546 +1200629@@ -1,3 +1,4 @@630 int main()631 {632+ return 0;633 }634'''635 with open('diff.patch', 'w', encoding='utf8') as patch_file:636 patch_file.write(patch)637 existing = 'some file content!\n'638 with open('a.orig', 'w', encoding='utf8') as existing_file:639 existing_file.write(existing)640 to_patch = '''int main()641{642}643'''644 with open('a', 'w', encoding='utf8') as to_patch_file:645 to_patch_file.write(to_patch)646 ret = run_patch('patch -b -i diff.patch')647 self.assertEqual(ret.stderr, '')648 self.assertEqual(ret.returncode, 0)649 self.assertEqual(ret.stdout, 'patching file a\n')650 self.assertFileEqual('a', '''int main()651{652 return 0;653}654''')655 self.assertFileEqual('a.orig', to_patch)656 def test_backup_multiple_files_only_backs_up_first(self):657 ''' test that if a patch changes same file twice, only the original is backed up '''658 patch = '''659--- a/main.cpp 2022-06-08 20:59:48.119369201 +1200660+++ b/main.cpp 2022-06-08 21:00:29.848423061 +1200661@@ -1,4 +1,3 @@662 int main()663 {664- return 0;665 }666--- b/main.cpp 2022-06-08 21:00:29.848423061 +1200667+++ c/main.cpp 2022-06-08 21:00:59.885147433 +1200668@@ -1,3 +1,4 @@669-int main()670+int main(int argc, char** argv)671 {672+ return 5; // and comment673 }674'''675 with open('diff.patch', 'w', encoding='utf8') as patch_file:676 patch_file.write(patch)677 to_patch = '''int main()678{679 return 0;680}681'''682 with open('main.cpp', 'w', encoding='utf8') as to_patch_file:683 to_patch_file.write(to_patch)684 ret = run_patch('patch -b -i diff.patch')685 self.assertEqual(ret.stderr, '')686 self.assertEqual(ret.returncode, 0)687 self.assertEqual(ret.stdout, 'patching file main.cpp\npatching file main.cpp\n')688 self.assertFileEqual('main.cpp.orig', to_patch)689 self.assertFileEqual('main.cpp', '''int main(int argc, char** argv)690{691 return 5; // and comment692}693''')694 def test_backup_prefix_only(self):695 ''' test applying a patch with backup prefix '''696 patch = '''697--- x 2022-07-27 21:07:04.624795529 +1200698+++ y 2022-07-27 21:07:08.304813552 +1200699@@ -1 +1 @@700-abc701+xyz702'''703 with open('diff.patch', 'w', encoding='utf8') as patch_file:704 patch_file.write(patch)705 to_patch = 'abc\n'706 with open('x', 'w', encoding='utf8') as to_patch_file:707 to_patch_file.write(to_patch)708 ret = run_patch('patch --backup --prefix pre. -i diff.patch')709 self.assertEqual(ret.returncode, 0)710 self.assertEqual(ret.stdout, 'patching file x\n')711 self.assertEqual(ret.stderr, '')712 self.assertFileEqual('pre.orig', 'abc\n')713 self.assertFileEqual('x', 'xyz\n')714 def test_backup_suffix_only(self):715 ''' test applying a patch with backup suffix '''716 patch = '''717--- x 2022-07-27 21:07:04.624795529 +1200718+++ y 2022-07-27 21:07:08.304813552 +1200719@@ -1 +1 @@720-abc721+xyz722'''723 with open('diff.patch', 'w', encoding='utf8') as patch_file:724 patch_file.write(patch)725 to_patch = 'abc\n'726 with open('x', 'w', encoding='utf8') as to_patch_file:727 to_patch_file.write(to_patch)728 ret = run_patch('patch --backup --suffix .post -i diff.patch')729 self.assertEqual(ret.returncode, 0)730 self.assertEqual(ret.stdout, 'patching file x\n')731 self.assertEqual(ret.stderr, '')732 self.assertFileEqual('x.post', 'abc\n')733 self.assertFileEqual('x', 'xyz\n')734 def test_backup_prefix_and_suffix(self):735 ''' test applying a patch with backup prefix and suffix '''736 patch = '''737--- x 2022-07-27 21:07:04.624795529 +1200738+++ y 2022-07-27 21:07:08.304813552 +1200739@@ -1 +1 @@740-abc741+xyz742'''743 with open('diff.patch', 'w', encoding='utf8') as patch_file:744 patch_file.write(patch)745 to_patch = 'abc\n'746 with open('x', 'w', encoding='utf8') as to_patch_file:747 to_patch_file.write(to_patch)748 ret = run_patch('patch --backup --prefix pre. --suffix .post -i diff.patch')749 self.assertEqual(ret.returncode, 0)750 self.assertEqual(ret.stdout, 'patching file x\n')751 self.assertEqual(ret.stderr, '')752 self.assertFileEqual('pre.x.post', 'abc\n')753 self.assertFileEqual('x', 'xyz\n')754 def test_reverse_still_applies_to_first_file(self):755 ''' test that reversing a file will still apply to first the reverse file '''756 patch = '''757--- a/x.cpp 2022-06-10 19:28:11.018017172 +1200758+++ b/y.cpp 2022-06-10 19:28:21.841903003 +1200759@@ -1,3 +1,4 @@760 int main()761 {762+ return 1;763 }764'''765 with open('diff.patch', 'w', encoding='utf8') as patch_file:766 patch_file.write(patch)767 to_patch = '''int main()768{769 return 1;770}771'''772 with open('x.cpp', 'w', encoding='utf8') as to_patch_file:773 to_patch_file.write(to_patch)774 with open('y.cpp', 'w', encoding='utf8') as to_patch_file:775 to_patch_file.write(to_patch)776 # GNU patch does not seem to also reverse the old and new file777 # names and will still apply the patch to the old file first.778 ret = run_patch('patch -R -i diff.patch')779 self.assertEqual(ret.stderr, '')780 self.assertEqual(ret.returncode, 0)781 self.assertEqual(ret.stdout, 'patching file x.cpp\n')782 self.assertFileEqual('y.cpp', to_patch)783 self.assertFileEqual('x.cpp', '''int main()784{785}786''')787 def test_read_patch_from_stdin(self):788 ''' test reading a patch from stdin works as intended '''789 patch = '''--- a790+++ b791@@ -1,5 +1,4 @@792 1793 2794-3795-4796 797+4798'''799 to_patch = '''1800280138024803'''804 for extra_arg in [' -i -', '']:805 with open('a', 'w', encoding='utf8') as to_patch_file:806 to_patch_file.write(to_patch)807 ret = run_patch('patch' + extra_arg, input=patch)808 self.assertEqual(ret.stderr, '')809 self.assertEqual(ret.stdout, 'patching file a\n')810 self.assertEqual(ret.returncode, 0)811 self.assertFileEqual('a', '''181228134814''')815 def test_write_output_to_stdout(self):816 ''' test that setting -o to - writes the patched file to stdout '''817 patch = '''818--- a/x.cpp 2022-06-10 19:28:11.018017172 +1200819+++ b/y.cpp 2022-06-10 19:28:21.841903003 +1200820@@ -1,3 +1,4 @@821 int main()822 {823+ return 1;824 }825'''826 with open('diff.patch', 'w', encoding='utf8') as patch_file:827 patch_file.write(patch)828 to_patch = '''int main()829{830}831'''832 with open('x.cpp', 'w', encoding='utf8') as to_patch_file:833 to_patch_file.write(to_patch)834 ret = run_patch('patch -i diff.patch -o -')835 self.assertEqual(ret.stderr, 'patching file - (read from x.cpp)\n')836 self.assertEqual(ret.returncode, 0)837 self.assertEqual(ret.stdout, '''\838int main()839{840 return 1;841}842''')843 def test_write_empty_output_to_stdout(self):844 ''' test that setting -o to - writes the patched file to stdout for an empty file '''845 patch = '''846--- a 2022-07-02 15:23:07.929349813 +1200847+++ /dev/null 2022-06-30 20:33:13.470250591 +1200848@@ -1 +0,0 @@849-1850'''851 with open('diff.patch', 'w', encoding='utf8') as patch_file:852 patch_file.write(patch)853 to_patch = '1\n'854 with open('a', 'w', encoding='utf8') as to_patch_file:855 to_patch_file.write(to_patch)856 ret = run_patch('patch -i diff.patch -o -')857 self.assertEqual(ret.stderr, 'patching file - (read from a)\n')858 self.assertEqual(ret.returncode, 0)859 self.assertEqual(ret.stdout, '')860 def test_write_output_to_some_file(self):861 ''' test overriding the file path to patch '''862 patch = '''863--- a 2022-08-21 12:01:05.302352795 +1200864+++ b 2022-08-21 12:01:08.874339091 +1200865@@ -1,3 +1,3 @@866 a867-b868+d869 c870'''871 with open('diff.patch', 'w', encoding='utf8') as patch_file:872 patch_file.write(patch)873 to_patch = 'a\nb\nc\n'874 with open('a', 'w', encoding='utf8') as to_patch_file:875 to_patch_file.write(to_patch)876 ret = run_patch('patch -i diff.patch -osome-file')877 self.assertEqual(ret.stdout, 'patching file some-file (read from a)\n')878 self.assertEqual(ret.stderr, '')879 self.assertEqual(ret.returncode, 0)880 self.assertFileEqual('a', 'a\nb\nc\n')881 self.assertFileEqual('some-file', 'a\nd\nc\n')882 def test_reverse_option_when_reversed(self):883 ''' test that we still check for reversed when reverse option specified '''884 patch = '''885--- main.cpp 2022-06-11 16:34:12.304745651 +1200886+++ main2.cpp 2022-06-11 16:34:04.240768394 +1200887@@ -1,4 +1,3 @@888 int main()889 {890- return 0;891 }892'''893 with open('diff.patch', 'w', encoding='utf8') as patch_file:894 patch_file.write(patch)895 to_patch = '''int main()896{897 return 0;898}899'''900 with open('main.cpp', 'w', encoding='utf8') as to_patch_file:901 to_patch_file.write(to_patch)902 ret = run_patch('patch -RN -i diff.patch')903 self.assertEqual(ret.stderr, '')904 # perhaps we should have a better error here?905 self.assertEqual(ret.stdout, '''patching file main.cpp906Unreversed patch detected! Skipping patch.907Hunk #1 skipped at 1 with fuzz 2.9081 out of 1 hunk ignored -- saving rejects to file main.cpp.rej909''')910 self.assertFileEqual('main.cpp.orig', '''int main()911{912 return 0;913}914''')915 self.assertFileEqual('main.cpp.rej', '''--- main2.cpp 2022-06-11 16:34:04.240768394 +1200916+++ main.cpp 2022-06-11 16:34:12.304745651 +1200917@@ -1,3 +1,4 @@918 int main()919 {920+ return 0;921 }922''')923 self.assertEqual(ret.returncode, 1)924 def test_both_patch_and_input_as_crlf_output_keep(self):925 ''' test handling when input is CRLF '''926 patch = '''927--- to_patch 2022-06-19 16:56:12.974516527 +1200928+++ to_patch 2022-06-19 16:56:24.666877199 +1200929@@ -1,3 +1,4 @@930 int main()\r931 {\r932+ return 0;\r933 }\r934'''935 with open('diff.patch', 'wb') as patch_file:936 patch_file.write(patch.encode('utf-8'))937 to_patch = '''int main()\r938{\r939}\r940'''941 with open('to_patch', 'wb') as to_patch_file:942 to_patch_file.write(to_patch.encode('utf-8'))943 ret = run_patch('patch -i diff.patch --newline-output preserve')944 self.assertEqual(ret.returncode, 0)945 self.assertEqual(ret.stdout, 'patching file to_patch\n')946 self.assertEqual(ret.stderr, '')947 self.assertFileBinaryEqual('to_patch', '''int main()\r948{\r949 return 0;\r950}\r951'''.encode('utf-8'))952 def test_mix_patch_and_input_as_crlf_with_preserve(self):953 ''' test handling when input is CRLF and newlines preserved '''954 patch = '''955--- to_patch 2022-06-19 16:56:12.974516527 +1200956+++ to_patch 2022-06-19 16:56:24.666877199 +1200957@@ -1,3 +1,4 @@958 int main()\r959 {\r960+ return 0;\r961 }\r962'''963 with open('diff.patch', 'wb') as patch_file:964 patch_file.write(patch.encode('utf-8'))965 to_patch = '''int main()966{967}968'''969 with open('to_patch', 'wb') as to_patch_file:970 to_patch_file.write(to_patch.encode('utf-8'))971 ret = run_patch('patch -i diff.patch --newline-output preserve')972 self.assertEqual(ret.returncode, 0)973 self.assertEqual(ret.stdout, '''patching file to_patch974Hunk #1 succeeded at 1 with fuzz 2.975''')976 self.assertEqual(ret.stderr, '')977 self.assertFileBinaryEqual('to_patch', '''int main()978{979 return 0;\r980}981'''.encode('utf-8'))982 def test_mix_patch_and_input_as_crlf_newlines_crlf(self):983 ''' test handling when input is CRLF and output is CRLF '''984 patch = '''985--- to_patch 2022-06-19 16:56:12.974516527 +1200986+++ to_patch 2022-06-19 16:56:24.666877199 +1200987@@ -1,3 +1,4 @@988 int main()\r989 {\r990+ return 0;\r991 }\r992'''993 with open('diff.patch', 'wb') as patch_file:994 patch_file.write(patch.encode('utf-8'))995 to_patch = '''int main()996{997}998'''999 with open('to_patch', 'wb') as to_patch_file:1000 to_patch_file.write(to_patch.encode('utf-8'))1001 ret = run_patch('patch -l -i diff.patch --newline-output crlf')1002 self.assertEqual(ret.returncode, 0)1003 self.assertEqual(ret.stdout, 'patching file to_patch\n')1004 self.assertEqual(ret.stderr, '')1005 self.assertFileBinaryEqual('to_patch', '''int main()\r1006{\r1007 return 0;\r1008}\r1009'''.encode('utf-8'))1010 def test_mix_patch_and_input_as_crlf_newlines_native(self):1011 ''' test that mixed newline output is written correctly as native '''1012 patch = '''1013--- to_patch 2022-06-19 16:56:12.974516527 +12001014+++ to_patch 2022-06-19 16:56:24.666877199 +12001015@@ -1,3 +1,4 @@1016 int main()\r1017 {1018+ return 0;\r1019 }\r1020'''1021 with open('diff.patch', 'wb') as patch_file:1022 patch_file.write(patch.encode('utf-8'))1023 to_patch = '''int main()1024{\r1025}1026'''1027 with open('to_patch', 'wb') as to_patch_file:1028 to_patch_file.write(to_patch.encode('utf-8'))1029 ret = run_patch('patch -l -i diff.patch --newline-output native')1030 self.assertEqual(ret.returncode, 0)1031 self.assertEqual(ret.stdout, 'patching file to_patch\n')1032 self.assertEqual(ret.stderr, '')1033 self.assertFileEqual('to_patch', '''int main()1034{1035 return 0;1036}1037''')1038 def test_mix_patch_and_input_as_crlf_newlines_lf(self):1039 ''' test handling when input is CRLF writing as LF '''1040 patch = '''1041--- to_patch 2022-06-19 16:56:12.974516527 +12001042+++ to_patch 2022-06-19 16:56:24.666877199 +12001043@@ -1,3 +1,4 @@1044 int main()\r1045 {\r1046+ return 0;\r1047 }\r1048'''1049 with open('diff.patch', 'wb') as patch_file:1050 patch_file.write(patch.encode('utf-8'))1051 to_patch = '''int main()1052{1053}1054'''1055 with open('to_patch', 'wb') as to_patch_file:1056 to_patch_file.write(to_patch.encode('utf-8'))1057 ret = run_patch('patch -l -i diff.patch --newline-output crlf')1058 self.assertEqual(ret.returncode, 0)1059 self.assertEqual(ret.stdout, 'patching file to_patch\n')1060 self.assertEqual(ret.stderr, '')1061 self.assertFileBinaryEqual('to_patch', '''int main()\r1062{\r1063 return 0;\r1064}\r1065'''.encode('utf-8'))1066 def test_mix_patch_and_input_as_crlf_ignore_whitespace(self):1067 ''' test handling when input is CRLF, but ignoring whitespace '''1068 patch = '''1069--- to_patch 2022-06-19 16:56:12.974516527 +12001070+++ to_patch 2022-06-19 16:56:24.666877199 +12001071@@ -1,3 +1,4 @@1072 int main()\r1073 {\r1074+ return 0;\r1075 }\r1076'''1077 with open('diff.patch', 'wb') as patch_file:1078 patch_file.write(patch.encode('utf-8'))1079 to_patch = '''int main()1080{1081}1082'''1083 with open('to_patch', 'wb') as to_patch_file:1084 to_patch_file.write(to_patch.encode('utf-8'))1085 ret = run_patch('patch --ignore-whitespace -i diff.patch --newline-output preserve')1086 self.assertEqual(ret.returncode, 0)1087 self.assertEqual(ret.stdout, 'patching file to_patch\n')1088 self.assertEqual(ret.stderr, '')1089 self.assertFileBinaryEqual('to_patch', '''int main()1090{1091 return 0;\r1092}1093'''.encode('utf-8'))1094 def test_patch_rename_no_change(self):1095 ''' test that patch correctly applies a rename '''1096 patch = '''diff --git a/orig_file b/another_new1097similarity index 100%1098rename from orig_file1099rename to another_new1100'''1101 with open('diff.patch', 'w', encoding='utf8') as patch_file:1102 patch_file.write(patch)1103 with open('orig_file', 'w', encoding='utf8') as to_patch_file:1104 to_patch_file.write('a\nb\nc\nd\n')1105 ret = run_patch('patch -i diff.patch')1106 self.assertEqual(ret.returncode, 0)1107 self.assertEqual(ret.stdout, 'patching file another_new (renamed from orig_file)\n')1108 self.assertEqual(ret.stderr, '')1109 self.assertFileEqual('another_new', 'a\nb\nc\nd\n')1110 self.assertFalse(os.path.exists('orig_file'))1111 def test_patch_reverse_rename_no_change(self):1112 ''' test that patch with reverse option correctly applies a rename '''1113 patch = '''diff --git a/x b/y1114similarity index 100%1115rename from x1116rename to y1117'''1118 with open('diff.patch', 'w', encoding='utf8') as patch_file:1119 patch_file.write(patch)1120 to_patch = 'a\nb\nc\nd\n'1121 with open('y', 'w', encoding='utf8') as to_patch_file:1122 to_patch_file.write(to_patch)1123 ret = run_patch('patch -i diff.patch -R')1124 self.assertEqual(ret.returncode, 0)1125 self.assertEqual(ret.stdout, 'patching file x (renamed from y)\n')1126 self.assertEqual(ret.stderr, '')1127 self.assertFileEqual('x', to_patch)1128 self.assertFalse(os.path.exists('y'))1129 def test_patch_rename_with_change(self):1130 ''' test that patch correctly applies a rename with hunk content '''1131 patch = '''diff --git a/file b/test1132similarity index 87%1133rename from thing1134rename to test1135index 71ac1b5..fc3102f 1006441136--- a/thing1137+++ b/test1138@@ -2,7 +2,6 @@ a1139 b1140 c1141 d1142-e1143 f1144 g1145 h1146'''1147 with open('diff.patch', 'w', encoding='utf8') as patch_file:1148 patch_file.write(patch)1149 with open('thing', 'w', encoding='utf8') as to_patch_file:1150 to_patch_file.write('''a1151b1152c1153d1154e1155f1156g1157h1158''')1159 ret = run_patch('patch -i diff.patch')1160 self.assertEqual(ret.returncode, 0)1161 self.assertEqual(ret.stdout, 'patching file test (renamed from thing)\n')1162 self.assertEqual(ret.stderr, '')1163 self.assertFileEqual('test', '''a1164b1165c1166d1167f1168g1169h1170''')1171 self.assertFalse(os.path.exists('thing'))1172 def test_patch_copy_no_change(self):1173 ''' test that patch correctly applies a copy with no hunk content '''1174 patch = '''diff --git a/x b/y1175similarity index 100%1176copy from x1177copy to y1178'''1179 with open('diff.patch', 'w', encoding='utf8') as patch_file:1180 patch_file.write(patch)1181 to_patch = '''int main()1182{1183 return 0;1184}1185'''1186 with open('x', 'w', encoding='utf8') as to_patch_file:1187 to_patch_file.write(to_patch)1188 ret = run_patch('patch -i diff.patch')1189 self.assertEqual(ret.stdout, 'patching file y (copied from x)\n')1190 self.assertEqual(ret.returncode, 0)1191 self.assertEqual(ret.stderr, '')1192 self.assertFileEqual('x', to_patch)1193 self.assertFileEqual('y', to_patch)1194 def test_patch_copy_with_change(self):1195 ''' test that patch correctly applies a copy with hunk content '''1196 patch = '''diff --git a/x b/y1197similarity index 51%1198copy from x1199copy to y1200index 905869d..2227c3a 1006441201--- a/x1202+++ b/y1203@@ -1,4 +1,4 @@1204 int main()1205 {1206- return 0;1207+ return 1;1208 }1209'''1210 with open('diff.patch', 'w', encoding='utf8') as patch_file:1211 patch_file.write(patch)1212 to_patch = '''int main()1213{1214 return 0;1215}1216'''1217 with open('x', 'w', encoding='utf8') as to_patch_file:1218 to_patch_file.write(to_patch)1219 ret = run_patch('patch -i diff.patch')1220 self.assertEqual(ret.returncode, 0)1221 self.assertEqual(ret.stdout, 'patching file y (copied from x)\n')1222 self.assertEqual(ret.stderr, '')1223 self.assertFileEqual('x', to_patch)1224 self.assertFileEqual('y', '''int main()1225{1226 return 1;1227}1228''')1229 def test_patch_rename_already_exists_no_content(self):1230 ''' patch is not applied for rename only '''1231 patch = '''1232From 89629b257f091dd0ff78509ca0ad626089defaa7 Mon Sep 17 00:00:00 20011233From: Shannon Booth <shannon.ml.booth@gmail.com>1234Date: Tue, 5 Jul 2022 18:53:32 +12001235Subject: [PATCH] move a to b1236---1237 a => b | 01238 1 file changed, 0 insertions(+), 0 deletions(-)1239 rename a => b (100%)1240diff --git a/a b/b1241similarity index 100%1242rename from a1243rename to b1244--12452.25.11246'''1247 with open('diff.patch', 'w', encoding='utf8') as patch_file:1248 patch_file.write(patch)1249 existing_b = '1\n2\n3\n'1250 with open('b', 'w', encoding='utf8') as to_patch_file:1251 to_patch_file.write(existing_b)1252 ret = run_patch('patch -i diff.patch')1253 self.assertEqual(ret.returncode, 0)1254 self.assertEqual(ret.stdout, 'patching file b (already renamed from a)\n')1255 self.assertEqual(ret.stderr, '')1256 self.assertFalse(os.path.exists('a'))1257 self.assertFileEqual('b', existing_b)1258 def test_patch_rename_already_exists_with_content(self):1259 ''' test rename patch where rename is made, but content of patch is still applied '''1260 patch = '''1261diff --git a/b b/a1262similarity index 66%1263rename from b1264rename to a1265index de98044..0f673f8 1006441266--- a/b1267+++ b/a1268@@ -1,3 +1,3 @@1269 a1270-b1271+21272 c1273'''1274 with open('diff.patch', 'w', encoding='utf8') as patch_file:1275 patch_file.write(patch)1276 existing_a = 'a\nb\nc\n'1277 with open('a', 'w', encoding='utf8') as to_patch_file:1278 to_patch_file.write(existing_a)1279 ret = run_patch('patch -i diff.patch')1280 self.assertEqual(ret.stdout, 'patching file a (already renamed from b)\n')1281 self.assertEqual(ret.stderr, '')1282 self.assertEqual(ret.returncode, 0)1283 self.assertFalse(os.path.exists('b'))1284 self.assertFileEqual('a', 'a\n2\nc\n')1285 def test_patch_revsered_rename_already_exists_with_content(self):1286 ''' test reversed rename patch where rename is made, but content of patch is still applied '''1287 patch = '''1288diff --git a/a b/b1289similarity index 66%1290rename from a1291rename to b1292index de98044..0f673f8 1006441293--- a/a1294+++ b/b1295@@ -1,3 +1,3 @@1296 a1297-b1298+21299 c1300'''1301 with open('diff.patch', 'w', encoding='utf8') as patch_file:1302 patch_file.write(patch)1303 existing_a = 'a\n2\nc\n'1304 with open('a', 'w', encoding='utf8') as to_patch_file:1305 to_patch_file.write(existing_a)1306 ret = run_patch('patch -i diff.patch --reverse')1307 self.assertEqual(ret.stdout, 'patching file a (already renamed from b)\n')1308 self.assertEqual(ret.stderr, '')1309 self.assertEqual(ret.returncode, 0)1310 self.assertFalse(os.path.exists('b'))1311 self.assertFileEqual('a', 'a\nb\nc\n')1312 def test_error_when_invalid_patch_given(self):1313 ''' test proper error is output when an invalid patch file is supplied '''1314 patch = '''1315 some1316 garbage!1317'''1318 with open('diff.patch', 'w', encoding='utf8') as patch_file:1319 patch_file.write(patch)1320 ret = run_patch('patch -i diff.patch')1321 self.assertEqual(ret.returncode, 2)1322 self.assertEqual(ret.stdout, '')1323 self.assertEqual(ret.stderr, 'patch: **** Unable to determine patch format\n')1324 def test_error_when_non_existent_patch_file_given(self):1325 ''' test that when patch is given a non-existent patch file is given an error is raised '''1326 self.assertFalse(os.path.exists('diff.patch')) # sanity check1327 ret = run_patch('patch -b -i diff.patch')1328 # windows returns in different case, we don't care.1329 if os.name == 'nt':1330 self.assertEqual(ret.stderr, 'patch: **** Unable to open patch file diff.patch: no such file or directory\n')1331 else:1332 self.assertEqual(ret.stderr, 'patch: **** Unable to open patch file diff.patch: No such file or directory\n')1333 self.assertEqual(ret.returncode, 2)1334 self.assertEqual(ret.stdout, '')1335 def test_chdir_good_case(self):1336 ''' test using chdir allows relative path to work '''1337 os.mkdir('folder')1338 patch = '''1339--- 1 2022-06-26 11:17:58.948060133 +12001340+++ 2 2022-06-26 11:18:03.500001858 +12001341@@ -1,3 +1,2 @@1342 11343-21344 31345'''1346 with open(os.path.join('folder', 'diff.patch'), 'w', encoding='utf8') as patch_file:1347 patch_file.write(patch)1348 to_patch = '''113492135031351'''1352 with open(os.path.join('folder', '1'), 'w', encoding='utf8') as to_patch_file:1353 to_patch_file.write(to_patch)1354 ret = run_patch('patch -i diff.patch -d folder')1355 self.assertEqual(ret.returncode, 0)1356 self.assertEqual(ret.stdout, 'patching file 1\n')1357 self.assertEqual(ret.stderr, '')1358 def test_chdir_unicode(self):1359 ''' test using chdir with non ascii '''1360 os.mkdir('गिलास')1361 patch = '''1362--- 1 2022-06-26 11:17:58.948060133 +12001363+++ 2 2022-06-26 11:18:03.500001858 +12001364@@ -1,3 +1,2 @@1365 11366-21367 31368'''1369 with open(os.path.join('गिलास', 'diff.patch'), 'w', encoding='utf8') as patch_file:1370 patch_file.write(patch)1371 to_patch = '''113722137331374'''1375 with open(os.path.join('गिलास', '1'), 'w', encoding='utf8') as to_patch_file:1376 to_patch_file.write(to_patch)1377 ret = run_patch('patch -i diff.patch -d गिलास')1378 self.assertEqual(ret.stderr, '')1379 self.assertEqual(ret.returncode, 0)1380 self.assertEqual(ret.stdout, 'patching file 1\n')1381 def test_add_executable_bit(self):1382 ''' test that a patch changing mode applies '''1383 patch = '''1384diff --git a/file b/file1385old mode 1006441386new mode 1007551387'''1388 with open('diff.patch', 'w', encoding='utf8') as patch_file:1389 patch_file.write(patch)1390 to_patch = '1\n2\n3\n'1391 with open('file', 'w', encoding='utf8') as to_patch_file:1392 to_patch_file.write(to_patch)1393 ret = run_patch('patch -i diff.patch')1394 self.assertEqual(ret.returncode, 0)1395 self.assertEqual(ret.stdout, 'patching file file\n')1396 self.assertEqual(ret.stderr, '')1397 self.assertFileEqual('file', to_patch)1398 self.assertTrue(os.access('file', os.X_OK))1399 def test_read_only_file_default(self):1400 ''' test patching a file which is read only still applies with a warning by default '''1401 patch = '''1402--- a/a 2022-09-18 09:59:59.586887443 +12001403+++ b/a 2022-09-18 10:00:04.410912780 +12001404@@ -1,4 +1,3 @@1405 11406 21407-31408 41409'''1410 with open('diff.patch', 'w', encoding='utf8') as patch_file:1411 patch_file.write(patch)1412 to_patch = '1\n2\n3\n4\n'1413 with open('a', 'w', encoding='utf8') as to_patch_file:1414 to_patch_file.write(to_patch)1415 # Make file read-only. Keep track of old mode.1416 mode = os.stat('a').st_mode1417 ro_mask = 0o777 ^ (stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH)1418 os.chmod('a', mode & ro_mask)1419 old_mode = os.stat('a').st_mode1420 ret = run_patch('patch -i diff.patch')1421 self.assertEqual(ret.stdout, 'File a is read-only; trying to patch anyway\npatching file a\n')1422 self.assertEqual(ret.stderr, '')1423 self.assertEqual(ret.returncode, 0)1424 self.assertFileEqual('a', '1\n2\n4\n')1425 # Ensure file permissions restored to original1426 new_mode = os.stat('a').st_mode1427 self.assertEqual(new_mode, old_mode)1428 def test_read_only_file_ignore(self):1429 ''' test patching a file which is read only still applies with no warning if ignore flag set '''1430 patch = '''1431--- a/a 2022-09-18 09:59:59.586887443 +12001432+++ b/a 2022-09-18 10:00:04.410912780 +12001433@@ -1,4 +1,3 @@1434 11435 21436-31437 41438'''1439 with open('diff.patch', 'w', encoding='utf8') as patch_file:1440 patch_file.write(patch)1441 to_patch = '1\n2\n3\n4\n'1442 with open('a', 'w', encoding='utf8') as to_patch_file:1443 to_patch_file.write(to_patch)1444 # Make file read-only. Keep track of old mode.1445 mode = os.stat('a').st_mode1446 ro_mask = 0o777 ^ (stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH)1447 os.chmod('a', mode & ro_mask)1448 old_mode = os.stat('a').st_mode1449 ret = run_patch('patch -i diff.patch --read-only ignore')1450 self.assertEqual(ret.stdout, 'patching file a\n')1451 self.assertEqual(ret.stderr, '')1452 self.assertEqual(ret.returncode, 0)1453 self.assertFileEqual('a', '1\n2\n4\n')1454 # Ensure file permissions restored to original1455 new_mode = os.stat('a').st_mode1456 self.assertEqual(new_mode, old_mode)1457 def test_read_only_file_fail(self):1458 ''' test patching a file which is read only fails if fail option is set '''1459 patch = '''\1460--- a 2022-09-18 09:59:59.586887443 +12001461+++ a 2022-09-18 10:00:04.410912780 +12001462@@ -1,4 +1,3 @@1463 11464 21465-31466 41467'''1468 with open('diff.patch', 'w', encoding='utf8') as patch_file:1469 patch_file.write(patch)1470 to_patch = '1\n2\n3\n4\n'1471 with open('a', 'w', encoding='utf8') as to_patch_file:1472 to_patch_file.write(to_patch)1473 # Make file read-only. Keep track of old mode.1474 mode = os.stat('a').st_mode1475 ro_mask = 0o777 ^ (stat.S_IWRITE | stat.S_IWGRP | stat.S_IWOTH)1476 os.chmod('a', mode & ro_mask)1477 old_mode = os.stat('a').st_mode1478 ret = run_patch('patch -i diff.patch --read-only fail')1479 self.assertEqual(ret.stderr, '')1480 self.assertEqual(ret.stdout, '''File a is read-only; refusing to patch14811 out of 1 hunk ignored -- saving rejects to file a.rej1482''')1483 self.assertEqual(ret.returncode, 1)1484 self.assertFileEqual('a', to_patch)1485 self.assertFileEqual('a.rej', patch)1486 # Ensure file permissions restored to original1487 new_mode = os.stat('a').st_mode1488 self.assertEqual(new_mode, old_mode)1489 def test_error_on_chdir_to_bad_directory(self):1490 ''' test that an appropriate error is shown on chdir to a non existent directory '''1491 patch = '''1492--- main.cpp 2022-06-11 16:34:12.304745651 +12001493+++ main2.cpp 2022-06-11 16:34:04.240768394 +12001494@@ -1,4 +1,3 @@1495 int main()1496 {1497- return 0;1498 }1499'''1500 with open('diff.patch', 'w', encoding='utf8') as patch_file:1501 patch_file.write(patch)1502 ret = run_patch('patch -i diff.patch -d bad_directory')1503 # windows returns in different case, we don't care.1504 if os.name == 'nt':1505 self.assertEqual(ret.stderr, 'patch: **** Unable to change to directory bad_directory: no such file or directory\n')1506 else:1507 self.assertEqual(ret.stderr, 'patch: **** Unable to change to directory bad_directory: No such file or directory\n')1508 self.assertEqual(ret.returncode, 2)1509 self.assertEqual(ret.stdout, '')1510 def test_git_binary_patch(self):1511 ''' test application of unsupported git binary patches '''1512 patch = '''1513From f933cb15f717a43ef1961d797874ca4a5650ff08 Mon Sep 17 00:00:00 20011514From: Shannon Booth <shannon.ml.booth@gmail.com>1515Date: Mon, 18 Jul 2022 10:16:19 +12001516Subject: [PATCH] add utf161517---1518 a.txt | Bin 0 -> 14 bytes1519 1 file changed, 0 insertions(+), 0 deletions(-)1520 create mode 100644 a.txt1521diff --git a/a.txt b/a.txt1522new file mode 1006441523index 0000000000000000000000000000000000000000..c193b2437ca5bca3eaee833d9cc40b04875da7421524GIT binary patch1525literal 141526ScmezWFOh+ZAqj|+ffxWJ!UIA81527literal 01528HcmV?d000011529--15302.25.11531'''1532 with open('diff.patch', 'w', encoding='utf8') as patch_file:1533 patch_file.write(patch)1534 ret = run_patch('patch -i diff.patch')1535 self.assertEqual(ret.returncode, 1)1536 self.assertEqual(ret.stdout, 'File a.txt: git binary diffs are not supported.\n')1537 self.assertEqual(ret.stderr, '')1538 @unittest.expectedFailure1539 def test_git_swap_files(self):1540 ''' test that git patch swapping files is correctly applied '''1541 patch = '''\1542diff --git a/a b/b1543rename from a1544rename to b1545diff --git a/b b/a1546rename from b1547rename to a1548'''1549 with open('diff.patch', 'w', encoding='utf8') as patch_file:1550 patch_file.write(patch)1551 a = '1\n2\n3\n'1552 with open('a', 'w', encoding='utf8') as a_file:1553 a_file.write(a)1554 b = 'a\nb\nc\n'1555 with open('b', 'w', encoding='utf8') as b_file:1556 b_file.write(b)1557 ret = run_patch('patch -i diff.patch')1558 self.assertEqual(ret.returncode, 0)1559 self.assertEqual(ret.stderr, '')1560 self.assertEqual(ret.stdout, '''\1561patching file b (renamed from a)1562patching file a (renamed from b)1563''')1564 self.assertFileEqual('a', b)1565 self.assertFileEqual('b', a)1566 def test_remove_git_submodule(self):1567 ''' test removing a git submodule fails as it is not a regular file '''1568 patch = '''diff --git a/.gitmodules b/.gitmodules1569index 066b99a..e69de29 1006441570--- a/.gitmodules1571+++ b/.gitmodules1572@@ -1,3 +0,0 @@1573-[submodule "libarchive"]1574- path = libarchive1575- url = https://github.com/libarchive/libarchive.git1576diff --git a/libarchive b/libarchive1577deleted file mode 1600001578index a45905b..00000001579--- a/libarchive...

Full Screen

Full Screen

test_remote_dimension.py

Source:test_remote_dimension.py Github

copy

Full Screen

...25FOLDER = './dim_current/'26conn = create_connection(verify_ssl=False)27class TestDimension(TestCase):28 """Test Redis Dimension code"""29 def assertFileEqual(self, file1, file2, **kwargs):30 return self.assertTrue(filecmp.cmp(file1, file2, shallow=False))31 def assertFrameEqual(self, df1, df2, **kwargs):32 return assert_frame_equal(df1, df2, check_names=True, check_like=True, **kwargs)33 def setUp(self):34 if not os.path.exists(BASELINE):35 os.makedirs(BASELINE)36 self.periods = 'periods_rpc_test'37 self.dims = Dimensions(conn=conn)38 self.dim = self.dims.get_dimension(name=self.periods, replace=False)39 return40 def test_001_load_hierarchy_main(self):41 df_main = pd.DataFrame(42 [43 [ROOT, 'Year'],44 ['Year', 'Q1'],45 ['Year', 'Q2'],46 ['Year', 'Q3'],47 ['Year', 'Q4'],48 ['Q1', 'January'],49 ['Q1', 'February'],50 ['Q1', 'March'],51 ['Q2', 'April'],52 ['Q2', 'May'],53 ['Q2', 'June'],54 ['Q3', 'July'],55 ['Q3', 'August'],56 ['Q3', 'September'],57 ['Q4', 'October'],58 ['Q4', 'November'],59 ['Q4', 'December'],60 ],61 columns=['ParentName', 'ChildName']62 )63 # Clear down the dimension and reload64 self.dim.clear()65 # main hierarchy66 df_results = self.dim.load_hierarchy_from_dataframe(df_main, 'ParentName', 'ChildName')67 df_results.to_csv(f'{FOLDER}df_main_load.csv', index=False)68 # Create a backup file to allow reloading in tests69 data = self.dims.backup(self.periods)70 with open(f'{FOLDER}periods.yaml', 'w') as file:71 file.write(data)72 self.assertFileEqual(f'{FOLDER}df_main_load.csv', f'{BASELINE}df_main_load.csv')73 return74 def test_002_save_hierarchy_main(self):75 # main hierarchy76 df = self.dim.save_hierarchy_to_dataframe(MAIN)77 df.drop(labels='index', axis=1, inplace=True)78 df.to_csv(f'{FOLDER}df_main_hierarchy.csv', index=False)79 self.assertFileEqual(f'{FOLDER}df_main_hierarchy.csv', f'{BASELINE}df_main_hierarchy.csv')80 return81 def test_003_load_hierarchy_halves(self):82 df_halves = pd.DataFrame(83 [84 [ROOT, 'H1', '~', 'halves'],85 [ROOT, 'H2', '~', 'halves'],86 ['H1', 'Q1', '+', 'halves'],87 ['H1', 'Q2', '+', 'halves'],88 ['H2', 'Q3', '+', 'halves'],89 ['H2', 'Q4', '+', 'halves'],90 ],91 columns=['ParentName', 'ChildName', 'ConsolidationType', 'Hierarchy']92 )93 # halves hierarchy94 df_results = self.dim.load_hierarchy_from_dataframe(df_halves, 'ParentName', 'ChildName',95 'ConsolidationType', hierarchy='Hierarchy')96 df_results.to_csv(f'{FOLDER}df_halves_load.csv', index=False)97 self.assertFileEqual(f'{FOLDER}df_halves_load.csv', f'{BASELINE}df_halves_load.csv')98 return99 def test_004_save_hierarchy_halves(self):100 # halves hierarchy101 df = self.dim.save_hierarchy_to_dataframe('halves')102 df.drop(labels='index', axis=1, inplace=True)103 df.to_csv(f'{FOLDER}df_halves_hierarchy.csv', index=False)104 self.assertFileEqual(f'{FOLDER}df_halves_hierarchy.csv', f'{BASELINE}df_halves_hierarchy.csv')105 return106 def test_005_load_hierarchy_financial(self):107 df_financial = pd.DataFrame(108 [109 [ROOT, 'YTD', '+', 'financial'],110 [ROOT, 'YTG', '+', 'financial'],111 ['YTD', 'January', '+', 'financial'],112 ['YTD', 'February', '+', 'financial'],113 ['YTD', 'March', '+', 'financial'],114 ['YTD', 'April', '+', 'financial'],115 ['YTG', 'May', '-', 'financial'],116 ['YTG', 'June', '-', 'financial'],117 ['YTG', 'July', '-', 'financial'],118 ['YTG', 'August', '-', 'financial'],119 ['YTG', 'September', '-', 'financial'],120 ['YTG', 'October', '-', 'financial'],121 ['YTG', 'November', '-', 'financial'],122 ['YTG', 'December', '-', 'financial'],123 ],124 columns=['ParentName', 'ChildName', 'ConsolidationType', 'Hierarchy']125 )126 # financial hierarchy127 df_results = self.dim.load_hierarchy_from_dataframe(df_financial, 'ParentName', 'ChildName',128 'ConsolidationType', hierarchy='Hierarchy')129 df_results.to_csv(f'{FOLDER}df_financial_load.csv', index=False)130 self.assertFileEqual(f'{FOLDER}df_financial_load.csv', f'{BASELINE}df_financial_load.csv')131 return132 def test_006_save_hierarchy_financial(self):133 # financial hierarchy134 df = self.dim.save_hierarchy_to_dataframe('financial')135 df.drop(labels='index', axis=1, inplace=True)136 df.to_csv(f'{FOLDER}df_financial_hierarchy.csv', index=False)137 self.assertFileEqual(f'{FOLDER}df_financial_hierarchy.csv', f'{BASELINE}df_financial_hierarchy.csv')138 return139 def test_007_load_hierarchy_errors(self):140 # This dataframe includes specific errors so check out the results dataframe141 df_test = pd.DataFrame(142 [143 ['', '', '+', 'main'],144 [' ', ' ', '+', 'main'],145 ['Q5', '', '+', 'main'],146 [np.NaN, np.NaN, '+', 'main'],147 [None, None, '+', 'main'],148 ['None', 'None', '+', 'main'],149 ['Q5', 'Q5', '+', 'main'],150 ['Q5', ROOT, '+', 'main'],151 ['Q5', 'Donk:tober', '+', 'main'],152 ['Donk:tober', 'Janusday', '+', 'main'],153 ['Year', 'Q5', '+', 'main'],154 ['Year', 'Q5', '+', 'main'],155 ['Q4', 'Badtober', '+', 'halves'],156 ['Q6', 'Craptober', '+', ''],157 ],158 columns=['ParentName', 'ChildName', 'ConsolidationType', 'Hierarchy']159 )160 df_results = self.dim.load_hierarchy_from_dataframe(df_test, 'ParentName', 'ChildName',161 'ConsolidationType', hierarchy='Hierarchy')162 df_results.to_csv(f'{FOLDER}df_complex_load.csv', index=False)163 self.assertFileEqual(f'{FOLDER}df_complex_load.csv', f'{BASELINE}df_complex_load.csv')164 return165 def test_008_load_save_aliases(self):166 df_aliases = pd.DataFrame(167 [168 ['Trimestre 1', 'French', 'Q1'],169 ['Trimestre 2', 'French', 'Q2'],170 ['Trimestre 3', 'French', 'Q3'],171 ['Trimestre 4', 'French', 'Q4'],172 ['Janvier', 'French', 'January'],173 ['Fevier', 'French', 'February'],174 ['Mars', 'French', 'March'],175 ['Avril', 'French', 'April'],176 ['Mai', 'French', 'May'],177 ['Juin', 'French', 'June'],178 ['Julliet', 'French', 'July'],179 ['Aout', 'French', 'August'],180 ['Septembre', 'French', 'September'],181 ['Octobre', 'French', 'October'],182 ['Novembre', 'French', 'November'],183 ['Decembre', 'French', 'December'],184 ['Haneri 1', 'Welsh', 'H1'],185 ['Haneri 2', 'Welsh', 'H2'],186 ['Ionawr', 'Welsh', 'January'],187 ['Chwefror', 'Welsh', 'February'],188 ['Mawrth', 'Welsh', 'March'],189 ['Ebrill', 'Welsh', 'April'],190 ['Mai', 'Welsh', 'May'],191 ['Mehefin', 'Welsh', 'June'],192 ['Gorffennaf', 'Welsh', 'July'],193 ['Awst', 'Welsh', 'August'],194 ['Medi', 'Welsh', 'September'],195 ['Hydref', 'Welsh', 'October'],196 ['Tachwedd', 'Welsh', 'November'],197 ['Rhagfyr', 'Welsh', 'December'],198 ['Январь', 'Russian', 'January'],199 ['Февраль', 'Russian', 'February'],200 ['Март', 'Russian', 'March'],201 ['Апрель', 'Russian', 'April'],202 ['Май', 'Russian', 'May'],203 ['Июнь', 'Russian', 'June'],204 ['Июль', 'Russian', 'July'],205 ['Август', 'Russian', 'August'],206 ['Сентябрь', 'Russian', 'September'],207 ['Октябрь', 'Russian', 'October'],208 ['Ноябрь', 'Russian', 'November'],209 ['Декабрь', 'Russian', 'December'],210 ['일월', 'Korean', 'January'],211 ['이월', 'Korean', 'February'],212 ['삼월', 'Korean', 'March'],213 ['사월', 'Korean', 'April'],214 ['오월', 'Korean', 'May'],215 ['유월', 'Korean', 'June'],216 ['칠월', 'Korean', 'July'],217 ['팔월', 'Korean', 'August'],218 ['구월', 'Korean', 'September'],219 ['시월', 'Korean', 'October'],220 ['십일월', 'Korean', 'November'],221 ['십이월', 'Korean', 'December'],222 ['☃️', 'Emoji', 'January'],223 ['💘', 'Emoji', 'February'],224 ['☘️', 'Emoji', 'March'],225 ['☔', 'Emoji', 'April'],226 ['🌺', 'Emoji', 'May'],227 ['🌞', 'Emoji', 'June'],228 ['🍦', 'Emoji', 'July'],229 ['🏖️', 'Emoji', 'August'],230 ['🍎', 'Emoji', 'September'],231 ['🎃', 'Emoji', 'October'],232 ['🍂', 'Emoji', 'November'],233 ['🎅', 'Emoji', 'December'],234 ],235 columns=['AliasValue', 'AliasName', 'NodeName']236 )237 # Aliases238 self.dim.load_aliases_from_dataframe(df_aliases, 'NodeName', 'AliasName', 'AliasValue')239 df = self.dim.save_aliases_to_dataframe(None)240 df.drop(labels='index', axis=1, inplace=True)241 df.sort_values(by=['name', 'node', 'value'], axis=0, inplace=True)242 df.to_csv(f'{FOLDER}df_aliases.csv', index=False)243 self.assertFileEqual(f'{FOLDER}df_aliases.csv', f'{BASELINE}df_aliases.csv')244 return245 def test_009_load_save_properties(self):246 df_properties = pd.DataFrame(247 [248 ['Magenta', 'Colour', ROOT],249 ['Purple', 'Colour', 'Year'],250 ['Red', 'Colour', 'Q1'],251 ['Orange', 'Colour', 'Q2'],252 ['Green', 'Colour', 'April'],253 ['Green', 'Colour', 'May'],254 ['Blue', 'Colour', 'July'],255 ['Blue', 'Colour', 'August'],256 ['Blue', 'Colour', 'September'],257 ['White', 'Colour', 'Q4'],258 ['Red', 'Colour', 'October'],259 ['Green', 'Colour', 'November'],260 ['Red', 'Colour', 'December'],261 ['Winter', 'Season', 'Q1'],262 ['Spring', 'Season', 'Q2'],263 ['Summer', 'Season', 'Q3'],264 ['Autumn', 'Season', 'Q4'],265 ],266 columns=['PropertyValue', 'PropertyName', 'NodeName']267 )268 # Properties269 self.dim.load_properties_from_dataframe(df_properties, 'NodeName', 'PropertyName', 'PropertyValue')270 df = self.dim.save_properties_to_dataframe(None)271 df.drop(labels='index', axis=1, inplace=True)272 df.sort_values(by=['name', 'node', 'value'], axis=0, inplace=True)273 df.to_csv(f'{FOLDER}df_properties.csv', index=False)274 self.assertFileEqual(f'{FOLDER}df_properties.csv', f'{BASELINE}df_properties.csv')275 return276 def test_010_load_save_values(self):277 df_values = pd.DataFrame(278 [279 [-10.0, 'Costs', 'January'],280 [-100.0, 'Costs', 'February'],281 [-1000.0, 'Costs', 'March'],282 [-20.0, 'Costs', 'April'],283 [-200.0, 'Costs', 'May'],284 [-2000.0, 'Costs', 'June'],285 [-30.0, 'Costs', 'July'],286 [-300.0, 'Costs', 'August'],287 [-3000.0, 'Costs', 'September'],288 [-40.0, 'Costs', 'October'],289 [-400.0, 'Costs', 'November'],290 [-4000.0, 'Costs', 'December'],291 [10.0, 'Profit', 'January'],292 [100.0, 'Profit', 'February'],293 [1000.0, 'Profit', 'March'],294 [20.0, 'Profit', 'April'],295 [200.0, 'Profit', 'May'],296 [2000.0, 'Profit', 'June'],297 [30.0, 'Profit', 'July'],298 [300.0, 'Profit', 'August'],299 [3000.0, 'Profit', 'September'],300 [40.0, 'Profit', 'October'],301 [400.0, 'Profit', 'November'],302 [4000.0, 'Profit', 'December'],303 ],304 columns=['Value', 'ValueName', 'NodeName']305 )306 # Values307 self.dim.load_values_from_dataframe(df_values, 'NodeName', 'ValueName', 'Value')308 df = self.dim.save_values_to_dataframe(None)309 df.drop(labels='index', axis=1, inplace=True)310 df.sort_values(by=['name', 'node', 'value'], axis=0, inplace=True)311 df.to_csv(f'{FOLDER}df_values.csv', index=False)312 self.assertFileEqual(f'{FOLDER}df_values.csv', f'{BASELINE}df_values.csv')313 return314 def test_011_get_hierarchy_dataframe(self):315 df = self.dim.get_hierarchy_dataframe(hierarchy=MAIN)316 df = df.reindex(columns=sorted(df.columns))317 df.to_csv(f'{FOLDER}df_get_hierarchy_main.csv', index=False)318 self.assertFileEqual(f'{FOLDER}df_get_hierarchy_main.csv', f'{BASELINE}df_get_hierarchy_main.csv')319 return320 def test_012_get_aliases_dataframe(self):321 df = self.dim.get_aliases_dataframe()322 df = df.reindex(columns=sorted(df.columns))323 df.sort_values(by=list(df.columns), axis=0, inplace=True)324 df.to_csv(f'{FOLDER}df_get_aliases.csv', index=False)325 self.assertFileEqual(f'{FOLDER}df_get_aliases.csv', f'{BASELINE}df_get_aliases.csv')326 return327 def test_013_get_attributes_dataframe(self):328 df = self.dim.get_attributes_dataframe()329 df.drop(labels='index', axis=1, inplace=True)330 df = df.reindex(columns=sorted(df.columns))331 df.sort_values(by=list(df.columns), axis=0, inplace=True)332 df.to_csv(f'{FOLDER}df_get_attributes.csv', index=False)333 self.assertFileEqual(f'{FOLDER}df_get_attributes.csv', f'{BASELINE}df_get_attributes.csv')334 return335 def test_014_get_consolidation_dataframe(self):336 df = self.dim.get_consolidation_dataframe('Costs', hierarchy=MAIN)337 df.to_csv(f'{FOLDER}df_get_consolidation_costs_main.csv', index=False)338 self.assertFileEqual(f'{FOLDER}df_get_consolidation_costs_main.csv', f'{BASELINE}df_get_consolidation_costs_main.csv')339 return340 def test_015_get_properties_dataframe(self):341 df = self.dim.get_properties_dataframe()342 df.drop(labels='index', axis=1, inplace=True)343 df = df.reindex(columns=sorted(df.columns))344 df.sort_values(by=list(df.columns), axis=0, inplace=True)345 df.to_csv(f'{FOLDER}df_get_properties.csv', index=False)346 self.assertFileEqual(f'{FOLDER}df_get_properties.csv', f'{BASELINE}df_get_properties.csv')347 return348 def test_016_get_values_dataframe(self):349 df = self.dim.get_values_dataframe()350 df = df.reindex(columns=sorted(df.columns))351 df.sort_values(by=list(df.columns), axis=0, inplace=True)352 df.to_csv(f'{FOLDER}df_get_values.csv', index=False)353 self.assertFileEqual(f'{FOLDER}df_get_values.csv', f'{BASELINE}df_get_values.csv')354 return355 def test_017_get_hierarchy_table(self):356 df = self.dim.hierarchy_table(hierarchy=MAIN)357 df = df.reindex(columns=sorted(df.columns))358 df.sort_values(by=list(df.columns), axis=0, inplace=True)359 df.to_csv(f'{FOLDER}df_get_hierarchy_table_main.csv', index=False)360 self.assertFileEqual(f'{FOLDER}df_get_hierarchy_table_main.csv', f'{BASELINE}df_get_hierarchy_table_main.csv')361 return362 def test_018_get_all_leaves(self):363 expected = ['April',364 'August',365 'December',366 'February',367 'January',368 'Janusday',369 'July',370 'June',371 'March',372 'May',373 'November',374 'October',375 'September']376 nodes = sorted(self.dim.get_all_leaves(hierarchy=MAIN))377 return self.assertListEqual(expected, nodes)378 def test_019_get_all_nodes(self):379 expected = ['!!root!!',380 'April',381 'August',382 'December',383 'Donk-tober',384 'February',385 'January',386 'Janusday',387 'July',388 'June',389 'March',390 'May',391 'November',392 'October',393 'Q1',394 'Q2',395 'Q3',396 'Q4',397 'Q5',398 'September',399 'Year']400 nodes = sorted(self.dim.get_all_nodes(hierarchy=MAIN))401 return self.assertListEqual(expected, nodes)402 def test_020_get_all_parents(self):403 expected = ['!!root!!', 'Donk-tober', 'Q1', 'Q2', 'Q3', 'Q4', 'Q5', 'Year']404 nodes = sorted(self.dim.get_all_parents(hierarchy=MAIN))405 return self.assertListEqual(expected, nodes)406 def test_021_get_ancestors(self):407 expected = [[0, 'February'], [1, 'Q1'], [2, 'Year'], [3, '!!root!!']]408 nodes = self.dim.get_ancestors('February', hierarchy=MAIN)409 return self.assertListEqual(expected, nodes)410 def test_022_get_ancestor_at_generation(self):411 expected = 'Year'412 node = self.dim.get_ancestor_at_generation('February', 1, hierarchy=MAIN)413 return self.assertEqual(expected, node)414 def test_023_get_ancestor_at_level(self):415 expected = 'Year'416 node = self.dim.get_ancestor_at_level('February', 2, hierarchy=MAIN)417 return self.assertEqual(expected, node)418 def test_024_get_bottom(self):419 expected = 'March'420 node = self.dim.get_bottom('Q1', hierarchy=MAIN)421 return self.assertEqual(expected, node)422 def test_025_get_top(self):423 expected = 'January'424 node = self.dim.get_top('Q1', hierarchy=MAIN)425 return self.assertEqual(expected, node)426 def test_026_get_down(self):427 expected = 'March'428 node = self.dim.get_down('Q1', 'February', hierarchy=MAIN)429 return self.assertEqual(expected, node)430 def test_027_get_up(self):431 expected = 'January'432 node = self.dim.get_up('Q1', 'February', hierarchy=MAIN)433 return self.assertEqual(expected, node)434 def test_028_get_children(self):435 expected = ['January', 'February', 'March']436 nodes = self.dim.get_children('Q1', hierarchy=MAIN)437 return self.assertListEqual(expected, nodes)438 def test_029_get_children_count(self):439 expected = 3440 count = self.dim.get_children_count('Q1', hierarchy=MAIN)441 return self.assertEqual(expected, count)442 def test_030_get_generation(self):443 expected = 2444 count = self.dim.get_generation('Q1', hierarchy=MAIN)445 return self.assertEqual(expected, count)446 def test_031_get_grandparent(self):447 expected = 'Year'448 node = self.dim.get_grandparent('February', hierarchy=MAIN)449 return self.assertEqual(expected, node)450 def test_032_get_leaves(self):451 expected = [[2, 'January'],452 [2, 'February'],453 [2, 'March'],454 [2, 'April'],455 [2, 'May'],456 [2, 'June'],457 [2, 'July'],458 [2, 'August'],459 [2, 'September'],460 [2, 'October'],461 [2, 'November'],462 [2, 'December'],463 [3, 'Janusday']]464 nodes = self.dim.get_leaves('Year', hierarchy=MAIN)465 return self.assertEqual(expected, nodes)466 def test_033_get_leaves_at_generation(self):467 expected = [[2, 'January'],468 [2, 'February'],469 [2, 'March'],470 [2, 'April'],471 [2, 'May'],472 [2, 'June'],473 [2, 'July'],474 [2, 'August'],475 [2, 'September'],476 [2, 'October'],477 [2, 'November'],478 [2, 'December']]479 nodes = self.dim.get_leaves_at_generation('Year', 2, hierarchy=MAIN)480 return self.assertEqual(expected, nodes)481 def test_034_get_leaves_at_level(self):482 expected = [[3, 'January'],483 [3, 'February'],484 [3, 'March'],485 [3, 'April'],486 [3, 'May'],487 [3, 'June'],488 [3, 'July'],489 [3, 'August'],490 [3, 'September'],491 [3, 'October'],492 [3, 'November'],493 [3, 'December']]494 nodes = self.dim.get_leaves_at_level('February', 0, hierarchy=MAIN)495 return self.assertEqual(expected, nodes)496 def test_035_get_parent(self):497 expected = 'Q1'498 nodes = self.dim.get_parent('February', hierarchy=MAIN)499 return self.assertEqual(expected, nodes)500 def test_036_get_parents(self):501 expected = [['financial', 'halves', 'main'], ['YTD', 'Q1', 'Q1']]502 nodes = self.dim.get_parents('February')503 return self.assertEqual(expected, nodes)504 def test_037_get_siblings(self):505 expected = ['January', 'February', 'March']506 nodes = self.dim.get_siblings('February', hierarchy=MAIN)507 return self.assertEqual(expected, nodes)508 def test_038_get_difference(self):509 expected = sorted(['Janusday', 'Year', 'Q5', 'Donk-tober'])510 nodes = sorted(self.dim.get_difference(['halves']))511 return self.assertEqual(expected, nodes)512 def test_039_get_intersection(self):513 expected = sorted(['!!root!!', 'April', 'August', 'December', 'February', 'January', 'July', 'June', 'March',514 'May', 'November', 'October', 'Q1', 'Q2', 'Q3', 'Q4', 'September'])515 nodes = sorted(self.dim.get_intersection(['halves']))516 return self.assertEqual(expected, nodes)517 def test_040_get_union(self):518 expected = sorted(['!!root!!', 'April', 'August', 'December', 'Donk-tober', 'February', 'H1', 'H2', 'January',519 'Janusday', 'July', 'June', 'March', 'May', 'November', 'October', 'Q1', 'Q2', 'Q3', 'Q4',520 'Q5', 'September', 'Year'])521 nodes = sorted(self.dim.get_union(['halves']))522 return self.assertEqual(expected, nodes)523 def test_041_add_node_to_alt(self):524 expected = 'H2'525 self.dim.add_node('H2', 'Q5', '+', hierarchy='halves', after='Q4')526 node = self.dim.get_parent('Q5', hierarchy='halves')527 return self.assertEqual(expected, node)528 def test_042_move_node_in_alt(self):529 expected = 'H1'530 self.dim.move_node('Q5', 'H1', hierarchy='halves', before='Q2')531 node = self.dim.get_parent('Q5', hierarchy='halves')532 return self.assertEqual(expected, node)533 def test_043_rename_node(self):534 expected = 'Q5'535 self.dim.rename_node('Donk-tober', 'Davetober')536 node = self.dim.get_parent('Davetober', hierarchy=MAIN)537 return self.assertEqual(expected, node)538 def test_044_delete_node(self):539 self.dim.delete_node('Year', 'Q5', hierarchy=MAIN)540 node = self.dim.node_exists('Q5')541 return self.assertFalse(node)542 def test_045_default_alias_dataframe(self):543 self.dim.set_default_aliases(primary='Welsh', secondary='French')544 df = self.dim.get_aliases_dataframe()545 df = df.reindex(columns=sorted(df.columns))546 df.sort_values(by=list(df.columns), axis=0, inplace=True)547 df.to_csv(f'{FOLDER}df_get_default_aliases.csv', index=False)548 self.assertFileEqual(f'{FOLDER}df_get_default_aliases.csv', f'{BASELINE}df_get_default_aliases.csv')549 pass550 def tearDown(self):551 self.dim = None...

Full Screen

Full Screen

test_coverage_badge.py

Source:test_coverage_badge.py Github

copy

Full Screen

...8from os.path import dirname, realpath, join9from os import listdir, remove10from pre_commit_hooks.coverage_badge import coverage_color, load_badge, make_badge, main11class TestCoverageBadge(unittest.TestCase):12 def assertFileEqual(self, expected: str, actual: str):13 try:14 with open(expected, "r") as expected_file:15 with open(actual, "r") as actual_file:16 expected_text = expected_file.read()17 actual_text = actual_file.read()18 self.assertEqual(expected_text, actual_text)19 except Exception as e:20 self.fail(str(e))21 def test_coverage_color_red(self):22 expected = "red"23 actual = coverage_color(10.0, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green")24 self.assertEqual(expected, actual)25 def test_coverage_color_orange(self):26 expected = "orange"27 actual = coverage_color(66.1, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green")28 self.assertEqual(expected, actual)29 def test_coverage_color_yellow(self):30 expected = "yellow"31 actual = coverage_color(86.5, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green")32 self.assertEqual(expected, actual)33 def test_coverage_color_green(self):34 expected = "green"35 actual = coverage_color(96.0, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green")36 self.assertEqual(expected, actual)37 def test_coverage_color_00ff00(self):38 expected = "00ff00"39 actual = coverage_color(96.0, "red < 50.0 < orange < 75.0 < yellow < 95.0 < 00ff00")40 self.assertEqual(expected, actual)41 def test_coverage_color_expected_1(self):42 with self.assertRaises(Exception) as context:43 coverage_color(96.0, "red < 50.0 < orange < 75.0 < yellow < 95.0")44 self.assertEqual("Invalid colors array", str(context.exception))45 def test_coverage_color_expected_2(self):46 with self.assertRaises(Exception) as context:47 coverage_color(96.0, "red < 50.0 < orange < 75.0 < yellow < five < green")48 self.assertEqual("could not convert string to float: 'five'", str(context.exception))49 def test_load_badge_default(self):50 path = dirname(realpath(__file__)) + "/files"51 actual = path + "/actual_badge.svg"52 expected = path + "/expected_badge.svg"53 try:54 load_badge(96.0, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green", actual)55 self.assertFileEqual(expected, actual)56 except Exception as e:57 self.fail(str(e))58 def test_load_badge_flat(self):59 path = dirname(realpath(__file__)) + "/files"60 actual = path + "/actual_badge_flat.svg"61 expected = path + "/expected_badge_flat.svg"62 try:63 load_badge(80.42226487523992, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green", actual, "style=flat")64 self.assertFileEqual(expected, actual)65 except Exception as e:66 self.fail(str(e))67 def test_make_badge(self):68 path = dirname(realpath(__file__)) + "/files"69 data = path + "/test.coverage"70 actual = path + "/actual_badge_flat.svg"71 expected = path + "/expected_badge_flat.svg"72 try:73 make_badge(data, actual, "red < 50.0 < orange < 75.0 < yellow < 95.0 < green", {"style": "flat"})74 self.assertFileEqual(expected, actual)75 except Exception as e:76 self.fail(str(e))77 def test_main(self):78 path = dirname(realpath(__file__)) + "/files"79 data = path + "/test.coverage"80 actual = path + "/actual_badge_flat.svg"81 expected = path + "/expected_badge_flat.svg"82 try:83 options = [84 "--input",85 data,86 "--output",87 actual,88 "--colors",89 "red < 50.0 < orange < 75.0 < yellow < 95.0 < green",90 "--style",91 "flat",92 ]93 main(options)94 self.assertFileEqual(expected, actual)95 except Exception as e:96 self.fail(str(e))97 @staticmethod98 def stopTestRun():99 path = dirname(realpath(__file__)) + "/files"100 for file in listdir(path):101 if file.startswith("actual_"):102 remove(join(path, file))103if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful