diff --git a/.gitattributes b/.gitattributes index fdade2f..cf96c40 100644 --- a/.gitattributes +++ b/.gitattributes @@ -2,3 +2,4 @@ tests export-ignore .editorconfig export-ignore .gitattributes export-ignore phpcs.xml export-ignore +tests/resources/eol/*.txt binary diff --git a/lib/jblond/Diff/File.php b/lib/jblond/Diff/File.php new file mode 100644 index 0000000..b14f575 --- /dev/null +++ b/lib/jblond/Diff/File.php @@ -0,0 +1,73 @@ +file = $file; + return; + } + if (!file_exists($file)) { + throw new InvalidArgumentException(); + } + $file = file($file); + if ($file === false) { + throw new InvalidArgumentException(); + } + $this->file = $file; + } + + /** + * return the last line from the file array + * @return false|mixed + */ + public function getLastLine() + { + return end($this->file); + } + + /** + * Bool return if the file has a new line at the end + * @return bool + */ + public function hasNewLineAtTheEnd(): bool + { + return (bool)preg_match('(\r\n|\r|\n)', $this->getLastLine()); + } + + /** + * Return the File Ending / EOL / EOF Type + * @return string + */ + public function getEOLType(): string + { + preg_match('(\r\n|\r|\n)', $this->getLastLine(), $matches); + switch ($matches[0] ?? '') { + case "\n": + return 'EOL type is Unix (LF)'; + case "\r": + return 'EOL type is Mac (CR)'; + case "\r\n": + return 'EOL type is Windows (CRLF)'; + default: + return '\ No newline at end of file'; + } + } +} diff --git a/tests/Diff/FileTest.php b/tests/Diff/FileTest.php new file mode 100644 index 0000000..4feb8d7 --- /dev/null +++ b/tests/Diff/FileTest.php @@ -0,0 +1,146 @@ +setFile('tests/resources/eol/mac.txt'); + $this->assertEquals( + 'EOL type is Mac (CR)', + $mac->getEOLType() + ); + + $unix = new File(); + $unix->setFile('tests/resources/eol/unix.txt'); + $this->assertEquals( + 'EOL type is Unix (LF)', + $unix->getEOLType() + ); + + $noEol = new File(); + $noEol->setFile('tests/resources/eol/no-eol.txt'); + $this->assertEquals( + '\ No newline at end of file', + $noEol->getEOLType() + ); + + $windows = new File(); + $windows->setFile('tests/resources/eol/windows.txt'); + $this->assertEquals( + 'EOL type is Windows (CRLF)', + $windows->getEOLType() + ); + + $a = new File(); + $a->setFile('tests/resources/a.txt'); + $this->assertEquals( + 'EOL type is Unix (LF)', + $a->getEOLType() + ); + } + + /** + * Bool test if the file has a line ending + */ + public function testHasNewLineAtTheEnd() + { + $mac = new File(); + $mac->setFile('tests/resources/eol/mac.txt'); + $this->assertEquals( + true, + $mac->hasNewLineAtTheEnd() + ); + + $unix = new File(); + $unix->setFile('tests/resources/eol/unix.txt'); + $this->assertEquals( + true, + $unix->hasNewLineAtTheEnd() + ); + + $noEol = new File(); + $noEol->setFile('tests/resources/eol/no-eol.txt'); + $this->assertEquals( + false, + $noEol->hasNewLineAtTheEnd() + ); + + $windows = new File(); + $windows->setFile('tests/resources/eol/windows.txt'); + $this->assertEquals( + true, + $windows->hasNewLineAtTheEnd() + ); + + $a = new File(); + $a->setFile('tests/resources/a.txt'); + $this->assertEquals( + true, + $a->hasNewLineAtTheEnd() + ); + + $b = new File(); + $b->setFile('tests/resources/b.txt'); + $this->assertEquals( + true, + $b->hasNewLineAtTheEnd() + ); + } + + /** + * Test get the last string from a file + */ + public function testGetLastLine() + { + $mac = new File(); + $mac->setFile('tests/resources/eol/mac.txt'); + $this->assertEquals( + "Lorem ipsum\r", + $mac->getLastLine() + ); + + $unix = new File(); + $unix->setFile('tests/resources/eol/unix.txt'); + $this->assertEquals( + "Lorem ipsum\n", + $unix->getLastLine() + ); + + $noEol = new File(); + $noEol->setFile('tests/resources/eol/no-eol.txt'); + $this->assertEquals( + "Lorem ipsum", + $noEol->getLastLine() + ); + + $windows = new File(); + $windows->setFile('tests/resources/eol/windows.txt'); + $this->assertEquals( + "Lorem ipsum\r\n", + $windows->getLastLine() + ); + } + + /** + * Test if the file exists + */ + public function testSetFile() + { + $this->expectException(InvalidArgumentException::class); + $file = new File(); + $file->setFile('foo.txt'); + } +} diff --git a/tests/resources/eol/mac.txt b/tests/resources/eol/mac.txt new file mode 100644 index 0000000..95b6a4b --- /dev/null +++ b/tests/resources/eol/mac.txt @@ -0,0 +1 @@ +Lorem ipsum \ No newline at end of file diff --git a/tests/resources/eol/no-eol.txt b/tests/resources/eol/no-eol.txt new file mode 100644 index 0000000..e5d3534 --- /dev/null +++ b/tests/resources/eol/no-eol.txt @@ -0,0 +1 @@ +Lorem ipsum \ No newline at end of file diff --git a/tests/resources/eol/unix.txt b/tests/resources/eol/unix.txt new file mode 100644 index 0000000..3be11c6 --- /dev/null +++ b/tests/resources/eol/unix.txt @@ -0,0 +1 @@ +Lorem ipsum diff --git a/tests/resources/eol/windows.txt b/tests/resources/eol/windows.txt new file mode 100644 index 0000000..2b3087b --- /dev/null +++ b/tests/resources/eol/windows.txt @@ -0,0 +1 @@ +Lorem ipsum