Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow CRLF files in /tests/fixtures/ directories #205

Merged
merged 2 commits into from
Sep 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on: [push, pull_request]

jobs:
test:
runs-on: ubuntu-18.04
runs-on: ubuntu-22.04

services:
postgres:
Expand Down
8 changes: 6 additions & 2 deletions locallib.php
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,19 @@ function local_codechecker_check_other_file($file, $xml) {
// Certain files are permitted lines of any length because they are
// Auto-generated.
$allowanylength = in_array(basename($file), array('install.xml')) ||
substr($file, -4, 4) === '.csv';
substr($file, -4, 4) === '.csv';
// We allow CRLF line endings in .csv tests/fixtures files (see #203).
$allowcrlf = substr($file, -4, 4) === '.csv' &&
strpos($file, '/tests/fixtures/') !== false;

$lines = file($file);
$index = 0;
$blankrun = 0;
$donecrlf = false;
foreach ($lines as $l) {
$index++;
// Incorrect [Windows] line ending.
if ((strpos($l, "\r\n") !== false) && empty($donecrlf)) {
if (!$donecrlf && !$allowcrlf && (strpos($l, "\r\n") !== false)) {
local_codechecker_add_problem($fileinxml, $file, $index, 'crlf');
$donecrlf = true;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/behat/ui.feature
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ Feature: Codechecker UI works as expected
| index2.php | Invalid path index2.php | Files found: 1 |
| local/codechecker/version.php | Well done! | Invalid path |
| local/codechecker/tests/ | checker/tests/locallib_test.php | Invalid path |
| local/codechecker/tests/ | Files found: 9 | Invalid path |
| local/codechecker/tests/ | Files found: 11 | Invalid path |
| local/codechecker/tests/locallib_test.php | Well done! | Invalid path |
| local/codechecker/tests/fixtures/behat/problem.php | Files found: 1 | Invalid path |
| local/codechecker/tests/fixtures/behat/problem.php | Total: 2 error(s) and 1 warning(s) | Well done! |
Expand All @@ -38,8 +38,8 @@ Feature: Codechecker UI works as expected

Examples:
| path | exclude | seen | notseen |
| local/codechecker/tests | */tests/fixtures/* | Files found: 2 | Invalid path |
| local/codechecker/tests/ | *one*, *moodle_* | Files found: 8 | Invalid path |
| local/codechecker/tests | */tests/fixtures/* | Files found: 3 | Invalid path |
| local/codechecker/tests/ | *one*, *moodle_* | Files found: 10 | Invalid path |
| local/codechecker/tests | */tests/fixtures/* | locallib_test.php | problem.php |
| local/codechecker/tests/ | *moodle_* | Line 1 of the opening comment | moodle_php |
| local/codechecker/tests/ | *moodle_* | fixtures/behat/phpcompat | /moodle_php |
Expand Down
13 changes: 6 additions & 7 deletions tests/coverage.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,17 @@
*/
return new class extends phpunit_coverage_info {
/** @var array The list of folders relative to the plugin root to include in coverage generation. */
protected $includelistfolders = [
'classes',
'moodle',
];
protected $includelistfolders = [];

/** @var array The list of files relative to the plugin root to include in coverage generation. */
protected $includelistfiles = [
'locallib.php'];
protected $includelistfiles = [];

/** @var array The list of folders relative to the plugin root to exclude from coverage generation. */
protected $excludelistfolders = [
'moodle/tests',
'MoodleCS',
'moodle',
'PHPCompatibility',
'phpcs',
];

/** @var array The list of files relative to the plugin root to exclude from coverage generation. */
Expand Down
3 changes: 3 additions & 0 deletions tests/fixtures/crlf.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
f1,f2,f3
1,2,3
4,5,6
3 changes: 3 additions & 0 deletions tests/fixtures2/crlf.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
f1,f2,f3
1,2,3
4,5,6
28 changes: 27 additions & 1 deletion tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ public function local_codechecker_find_other_files_provider() {
* @param string[] $nomatches list of substring-matching strings not expected to be in the results.
*
* @dataProvider local_codechecker_find_other_files_provider()
* @covers ::local_codechecker_find_other_files()
* @covers ::local_codechecker_find_other_files
*/
public function test_local_codechecker_find_other_files(string $path, array $ignores,
array $extensions, array $matches, array $nomatches) {
Expand Down Expand Up @@ -143,4 +143,30 @@ public function test_local_codechecker_find_other_files(string $path, array $ign
$this->assertStringNotContainsString($nomatch, $results);
}
}

/**
* Verify test_local_codechecker_check_other_file() behaviour.
*
* @covers ::local_codechecker_check_other_file
*/
public function test_local_codechecker_check_other_file() {

require_once(__DIR__ . '/../locallib.php');

// Verify that lf files are ok.
$xml = new \SimpleXMLElement('<xml/>');
local_codechecker_check_other_file(__DIR__ . '/../version.php', $xml);
$this->assertStringContainsString('errors="0" warnings="0"', $xml->asXML());

// Verify crlf files in /tests/fixtures/ locations are ok.
$xml = new \SimpleXMLElement('<xml/>');
local_codechecker_check_other_file(__DIR__ . '/fixtures/crlf.csv', $xml);
$this->assertStringContainsString('errors="0" warnings="0"', $xml->asXML());

// Verify crlf files in not in /tests/fixtures/ locations are wrong.
$xml = new \SimpleXMLElement('<xml/>');
local_codechecker_check_other_file(__DIR__ . '/fixtures2/crlf.csv', $xml);
$this->assertStringContainsString('errors="1" warnings="0"', $xml->asXML());
$this->assertStringContainsString('Windows (CRLF) line ending instead of just LF', $xml->asXML());
}
}