From 369075828e13e37a65a2f8062a74e89f98dd3fac Mon Sep 17 00:00:00 2001 From: Patrick Brown Date: Wed, 6 May 2015 18:21:17 -0400 Subject: Append to BZip2 files. Unit tests for writing files. --- _test/tests/inc/io_deletefromfile.test.php | 42 +++++++++++++++++++++++++ _test/tests/inc/io_savefile.test.php | 49 ++++++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 _test/tests/inc/io_deletefromfile.test.php create mode 100644 _test/tests/inc/io_savefile.test.php (limited to '_test') diff --git a/_test/tests/inc/io_deletefromfile.test.php b/_test/tests/inc/io_deletefromfile.test.php new file mode 100644 index 000000000..361c82214 --- /dev/null +++ b/_test/tests/inc/io_deletefromfile.test.php @@ -0,0 +1,42 @@ +markTestSkipped('skipping all zlib tests. Need zlib extension'); + } + } + + /* + * dependency for tests needing zlib extension to pass + */ + public function test_ext_bz2() { + if (!extension_loaded('bz2')) { + $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); + } + } + + function _write($file){ + $contents = "The\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; + io_saveFile($file, $contents); + $this->assertTrue(io_deleteFromFile($file, "Delete\012")); + $this->assertEquals("The\012Delete01\012Delete02\012DeleteX\012Test\012", io_readFile($file)); + $this->assertTrue(io_deleteFromFile($file, "#Delete\\d+\012#", true)); + $this->assertEquals("The\012DeleteX\012Test\012", io_readFile($file)); + } + + function test_delete(){ + $this->_write(TMP_DIR.'/test.txt'); + } + +// /** +// * @depends test_ext_zlib +// */ +// function test_gzwrite(){ +// } + +} diff --git a/_test/tests/inc/io_savefile.test.php b/_test/tests/inc/io_savefile.test.php new file mode 100644 index 000000000..4a4d4671d --- /dev/null +++ b/_test/tests/inc/io_savefile.test.php @@ -0,0 +1,49 @@ +markTestSkipped('skipping all zlib tests. Need zlib extension'); + } + } + + /* + * dependency for tests needing zlib extension to pass + */ + public function test_ext_bz2() { + if (!extension_loaded('bz2')) { + $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); + } + } + + function _write($file){ + $contents = "The\012Write\012Test\012"; + $this->assertTrue(io_saveFile($file, $contents)); + $this->assertEquals($contents, io_readFile($file)); + $this->assertTrue(io_saveFile($file, $contents, true)); + $this->assertEquals($contents.$contents, io_readFile($file)); + } + + function test_write(){ + $this->_write(TMP_DIR.'/test.txt'); + } + + /** + * @depends test_ext_zlib + */ + function test_gzwrite(){ + $this->_write(TMP_DIR.'/test.txt.gz'); + } + + /** + * @depends test_ext_bz2 + */ + function test_bzwrite(){ + $this->_write(TMP_DIR.'/test.txt.bz2'); + } + +} -- cgit v1.2.3 From cfb71e37ca8859c4ad9a1db73de0a293ffc7a902 Mon Sep 17 00:00:00 2001 From: Patrick Brown Date: Wed, 6 May 2015 23:31:39 -0400 Subject: Deleting lines works with BZ2 files. --- _test/tests/inc/io_deletefromfile.test.php | 18 +++++++++++++----- _test/tests/inc/io_readfile.test.php | 7 ++++++- _test/tests/inc/io_readfile/large.txt.bz2 | Bin 0 -> 47 bytes _test/tests/inc/io_readfile/long.txt.bz2 | Bin 0 -> 53 bytes 4 files changed, 19 insertions(+), 6 deletions(-) create mode 100644 _test/tests/inc/io_readfile/large.txt.bz2 create mode 100644 _test/tests/inc/io_readfile/long.txt.bz2 (limited to '_test') diff --git a/_test/tests/inc/io_deletefromfile.test.php b/_test/tests/inc/io_deletefromfile.test.php index 361c82214..63951f548 100644 --- a/_test/tests/inc/io_deletefromfile.test.php +++ b/_test/tests/inc/io_deletefromfile.test.php @@ -33,10 +33,18 @@ class io_deletefromfile_test extends DokuWikiTest { $this->_write(TMP_DIR.'/test.txt'); } -// /** -// * @depends test_ext_zlib -// */ -// function test_gzwrite(){ -// } + /** + * @depends test_ext_zlib + */ + function test_gzwrite(){ + $this->_write(TMP_DIR.'/test.txt.gz'); + } + + /** + * @depends test_ext_bz2 + */ + function test_bzwrite(){ + $this->_write(TMP_DIR.'/test.txt.bz2'); + } } diff --git a/_test/tests/inc/io_readfile.test.php b/_test/tests/inc/io_readfile.test.php index e3e90cd8d..700c1902b 100644 --- a/_test/tests/inc/io_readfile.test.php +++ b/_test/tests/inc/io_readfile.test.php @@ -48,6 +48,11 @@ class io_readfile_test extends DokuWikiTest { $this->assertEquals("The\015\012Test\015\012", io_readFile(__DIR__.'/io_readfile/test.txt.bz2', false)); $this->assertEquals(false, io_readFile(__DIR__.'/io_readfile/nope.txt.bz2')); $this->assertEquals(false, io_readFile(__DIR__.'/io_readfile/corrupt.txt.bz2')); + // internal bzfile function + $this->assertEquals(array("The\015\012","Test\015\012"), bzfile(__DIR__.'/io_readfile/test.txt.bz2', true)); + $this->assertEquals(array_fill(0, 120, str_repeat('a', 80)."\012"), bzfile(__DIR__.'/io_readfile/large.txt.bz2', true)); + $line = str_repeat('a', 8888)."\012"; + $this->assertEquals(array($line,"\012",$line,"!"), bzfile(__DIR__.'/io_readfile/long.txt.bz2', true)); } -} \ No newline at end of file +} diff --git a/_test/tests/inc/io_readfile/large.txt.bz2 b/_test/tests/inc/io_readfile/large.txt.bz2 new file mode 100644 index 000000000..3135435f8 Binary files /dev/null and b/_test/tests/inc/io_readfile/large.txt.bz2 differ diff --git a/_test/tests/inc/io_readfile/long.txt.bz2 b/_test/tests/inc/io_readfile/long.txt.bz2 new file mode 100644 index 000000000..fb40759e6 Binary files /dev/null and b/_test/tests/inc/io_readfile/long.txt.bz2 differ -- cgit v1.2.3 From 6c0002048504e43b399abece0668afa2b5c87a07 Mon Sep 17 00:00:00 2001 From: Patrick Brown Date: Fri, 8 May 2015 17:11:44 -0400 Subject: Limit number of lines to replace. --- _test/tests/inc/io_replaceinfile.test.php | 58 +++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 _test/tests/inc/io_replaceinfile.test.php (limited to '_test') diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php new file mode 100644 index 000000000..98f21868f --- /dev/null +++ b/_test/tests/inc/io_replaceinfile.test.php @@ -0,0 +1,58 @@ +markTestSkipped('skipping all zlib tests. Need zlib extension'); + } + } + + /* + * dependency for tests needing zlib extension to pass + */ + public function test_ext_bz2() { + if (!extension_loaded('bz2')) { + $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); + } + } + + function _write($file){ + $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; + io_saveFile($file, $contents); + // Replace one, no regex + $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete00\012", false, 1)); + $this->assertEquals("The\012Delete00\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file)); + // Replace all, no regex + $this->assertTrue(io_replaceInFile($file, "Delete\012", "DeleteX\012", false, -1)); + $this->assertEquals("The\012Delete00\012DeleteX\012Delete01\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file)); + // Replace two, regex and backreference + $this->assertTrue(io_replaceInFile($file, "#Delete(\\d+)\012#", "\\1\012", true, 2)); + $this->assertEquals("The\01200\012DeleteX\01201\012Delete02\012DeleteX\012DeleteX\012Test\012", io_readFile($file)); + // Delete and insert, no regex + $this->assertTrue(io_replaceInFile($file, "DeleteX\012", "Replace\012", false, 0)); + $this->assertEquals("The\01200\01201\012Delete02\012Test\012Replace\012", io_readFile($file)); + } + + function test_replace(){ + $this->_write(TMP_DIR.'/test.txt'); + } + + /** + * @depends test_ext_zlib + */ + function test_gzwrite(){ + $this->_write(TMP_DIR.'/test.txt.gz'); + } + + /** + * @depends test_ext_bz2 + */ + function test_bzwrite(){ + $this->_write(TMP_DIR.'/test.txt.bz2'); + } + +} -- cgit v1.2.3 From 0c26fb18171bc6264c1b0b2dbdfddc34de5d579e Mon Sep 17 00:00:00 2001 From: Patrick Brown Date: Fri, 8 May 2015 17:13:32 -0400 Subject: Don't run redundant tests io_deleteFromFile is a special case of io_replaceInFile. Since the io_replace tests check compressed files, it doesn't have to be done in the io_delete test. --- _test/tests/inc/io_deletefromfile.test.php | 39 ++---------------------------- 1 file changed, 2 insertions(+), 37 deletions(-) (limited to '_test') diff --git a/_test/tests/inc/io_deletefromfile.test.php b/_test/tests/inc/io_deletefromfile.test.php index 63951f548..e86150aac 100644 --- a/_test/tests/inc/io_deletefromfile.test.php +++ b/_test/tests/inc/io_deletefromfile.test.php @@ -2,25 +2,8 @@ class io_deletefromfile_test extends DokuWikiTest { - /* - * dependency for tests needing zlib extension to pass - */ - public function test_ext_zlib() { - if (!extension_loaded('zlib')) { - $this->markTestSkipped('skipping all zlib tests. Need zlib extension'); - } - } - - /* - * dependency for tests needing zlib extension to pass - */ - public function test_ext_bz2() { - if (!extension_loaded('bz2')) { - $this->markTestSkipped('skipping all bzip2 tests. Need bz2 extension'); - } - } - - function _write($file){ + function test_delete(){ + $file = TMP_DIR.'/test.txt'; $contents = "The\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; io_saveFile($file, $contents); $this->assertTrue(io_deleteFromFile($file, "Delete\012")); @@ -29,22 +12,4 @@ class io_deletefromfile_test extends DokuWikiTest { $this->assertEquals("The\012DeleteX\012Test\012", io_readFile($file)); } - function test_delete(){ - $this->_write(TMP_DIR.'/test.txt'); - } - - /** - * @depends test_ext_zlib - */ - function test_gzwrite(){ - $this->_write(TMP_DIR.'/test.txt.gz'); - } - - /** - * @depends test_ext_bz2 - */ - function test_bzwrite(){ - $this->_write(TMP_DIR.'/test.txt.bz2'); - } - } -- cgit v1.2.3 From 9a734b7aaba1445e06c1ccb95e59f54e01688d45 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Thu, 28 May 2015 16:37:32 +0100 Subject: Refactor code to make it simpler. The changes should also: - fix unlikely edge case when replacement line is the same as the old line (would have resulted in timeout) - reduce memory footprint - avoid applying string search beyond maxlines replacement limit --- _test/tests/inc/io_replaceinfile.test.php | 28 ++++++++++++++++++++++++++-- 1 file changed, 26 insertions(+), 2 deletions(-) (limited to '_test') diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php index 98f21868f..c2dbc0dd4 100644 --- a/_test/tests/inc/io_replaceinfile.test.php +++ b/_test/tests/inc/io_replaceinfile.test.php @@ -2,6 +2,8 @@ class io_replaceinfile_test extends DokuWikiTest { + protected $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; + /* * dependency for tests needing zlib extension to pass */ @@ -21,8 +23,8 @@ class io_replaceinfile_test extends DokuWikiTest { } function _write($file){ - $contents = "The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012"; - io_saveFile($file, $contents); + + io_saveFile($file, $this->contents); // Replace one, no regex $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete00\012", false, 1)); $this->assertEquals("The\012Delete00\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file)); @@ -41,6 +43,7 @@ class io_replaceinfile_test extends DokuWikiTest { $this->_write(TMP_DIR.'/test.txt'); } + /** * @depends test_ext_zlib */ @@ -55,4 +58,25 @@ class io_replaceinfile_test extends DokuWikiTest { $this->_write(TMP_DIR.'/test.txt.bz2'); } + /** + * + */ + function test_edgecase1() + { + $file = TMP_DIR . '/test.txt'; + // Replace all, no regex, backreference like construct in replacement line + io_saveFile($file, $this->contents); + $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\\00\012", false, -1)); + $this->assertEquals("The\012Delete\\00\012Delete\\00\012Delete01\012Delete02\012Delete\\00\012DeleteX\012Test\012", io_readFile($file), "Edge case: backreference like construct in replacement line"); + } + /** + * @small + */ + function test_edgecase2() { + $file = TMP_DIR.'/test.txt'; + // Replace all, no regex, replacement line == search line + io_saveFile($file, $this->contents); + $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1)); + $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line"); + } } -- cgit v1.2.3 From 3dfe7d64760baa568018c1d6d311c26d1a2da098 Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Fri, 29 May 2015 16:52:05 +0100 Subject: add anchors when constructing pattern from a non-regex oldline --- _test/tests/inc/io_replaceinfile.test.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to '_test') diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php index c2dbc0dd4..de7301fa1 100644 --- a/_test/tests/inc/io_replaceinfile.test.php +++ b/_test/tests/inc/io_replaceinfile.test.php @@ -79,4 +79,17 @@ class io_replaceinfile_test extends DokuWikiTest { $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1)); $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line"); } + + /** + * + */ + function test_edgecase3() + { + $file = TMP_DIR . '/test.txt'; + $contents = "The\012Delete\01201Delete\01202Delete\012Test\012"; + // Replace all, no regex, oldline exactly matches one line; matches part of other lines - only the exact match should be replaced + io_saveFile($file, $contents); + $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1)); + $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines"); + } } -- cgit v1.2.3 From e12c5ac781d560502d478775502df70cd80472de Mon Sep 17 00:00:00 2001 From: Christopher Smith Date: Fri, 29 May 2015 16:55:23 +0100 Subject: Minor Refactoring - put test comments in more appropriate spot - move appending replacement line alongside its search/delete code --- _test/tests/inc/io_replaceinfile.test.php | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to '_test') diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php index de7301fa1..597138a20 100644 --- a/_test/tests/inc/io_replaceinfile.test.php +++ b/_test/tests/inc/io_replaceinfile.test.php @@ -59,35 +59,37 @@ class io_replaceinfile_test extends DokuWikiTest { } /** - * + * Test for a non-regex replacement where $newline contains a backreference like construct - it shouldn't affect the replacement */ function test_edgecase1() { $file = TMP_DIR . '/test.txt'; - // Replace all, no regex, backreference like construct in replacement line + io_saveFile($file, $this->contents); $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\\00\012", false, -1)); $this->assertEquals("The\012Delete\\00\012Delete\\00\012Delete01\012Delete02\012Delete\\00\012DeleteX\012Test\012", io_readFile($file), "Edge case: backreference like construct in replacement line"); } /** + * Test with replace all where replacement line == search line - must not timeout + * * @small */ function test_edgecase2() { $file = TMP_DIR.'/test.txt'; - // Replace all, no regex, replacement line == search line + io_saveFile($file, $this->contents); $this->assertTrue(io_replaceInFile($file, "Delete\012", "Delete\012", false, -1)); $this->assertEquals("The\012Delete\012Delete\012Delete01\012Delete02\012Delete\012DeleteX\012Test\012", io_readFile($file), "Edge case: new line the same as old line"); } /** - * + * Test where $oldline exactly matches one line and also matches part of other lines - only the exact match should be replaced */ function test_edgecase3() { $file = TMP_DIR . '/test.txt'; $contents = "The\012Delete\01201Delete\01202Delete\012Test\012"; - // Replace all, no regex, oldline exactly matches one line; matches part of other lines - only the exact match should be replaced + io_saveFile($file, $contents); $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1)); $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines"); -- cgit v1.2.3 From dc4a4eb00d67d7d28fae137437900220920577d4 Mon Sep 17 00:00:00 2001 From: Patrick Brown Date: Fri, 29 May 2015 15:38:43 -0400 Subject: Abort io_replaceInLine when the search parameter is empty --- _test/tests/inc/io_replaceinfile.test.php | 11 +++++++++++ 1 file changed, 11 insertions(+) (limited to '_test') diff --git a/_test/tests/inc/io_replaceinfile.test.php b/_test/tests/inc/io_replaceinfile.test.php index 597138a20..452ed7401 100644 --- a/_test/tests/inc/io_replaceinfile.test.php +++ b/_test/tests/inc/io_replaceinfile.test.php @@ -94,4 +94,15 @@ class io_replaceinfile_test extends DokuWikiTest { $this->assertTrue(io_replaceInFile($file, "Delete\012", "Replace\012", false, -1)); $this->assertEquals("The\012Replace\01201Delete\01202Delete\012Test\012", io_readFile($file), "Edge case: old line is a match for parts of other lines"); } + + /** + * Test passing an invalid parameter. + * + * @expectedException PHPUnit_Framework_Error_Warning + */ + function test_badparam() + { + /* The empty $oldline parameter should be caught before the file doesn't exist test. */ + $this->assertFalse(io_replaceInFile(TMP_DIR.'/not_existing_file.txt', '', '', false, 0)); + } } -- cgit v1.2.3