Skip to content

Commit

Permalink
Merge pull request #49 from maths/regex-fix
Browse files Browse the repository at this point in the history
Regex fix
  • Loading branch information
EJMFarrow authored Jun 6, 2024
2 parents 4c4e5bd + 2e412f2 commit 886de84
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 9 deletions.
4 changes: 4 additions & 0 deletions classes/cli_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,10 @@ public function validate_and_clean_args(): void {
}
}
if (isset($cliargs['ignorecat'])) {
// Allow escaping of forward slash at start in Windows.
if (strlen($cliargs['ignorecat']) > 1 && substr($cliargs['ignorecat'], 0, 2) === '//') {
$cliargs['ignorecat'] = substr($cliargs['ignorecat'], 1);
}
if (@preg_match($cliargs['ignorecat'], 'zzzzzzzz') === false) {
echo "\nThere is a problem with your regular expression for ignoring categories:\n";
echo error_get_last()["message"] . "\n";
Expand Down
2 changes: 1 addition & 1 deletion cli/createrepo.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@
[
'longopt' => 'ignorecat',
'shortopt' => 'x',
'description' => 'Regex of categories to ignore',
'description' => 'Regex of categories to ignore - add an extra leading / for Windows.',
'default' => $ignorecat,
'variable' => 'ignorecat',
'valuerequired' => true,
Expand Down
2 changes: 1 addition & 1 deletion cli/deletefrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
[
'longopt' => 'ignorecat',
'shortopt' => 'x',
'description' => 'Regex of categories to ignore',
'description' => 'Regex of categories to ignore - add an extra leading / for Windows.',
'default' => $ignorecat,
'variable' => 'ignorecat',
'valuerequired' => true,
Expand Down
2 changes: 1 addition & 1 deletion cli/exportrepofrommoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@
[
'longopt' => 'ignorecat',
'shortopt' => 'x',
'description' => 'Regex of categories to ignore',
'description' => 'Regex of categories to ignore - add an extra leading / for Windows.',
'default' => $ignorecat,
'variable' => 'ignorecat',
'valuerequired' => true,
Expand Down
2 changes: 1 addition & 1 deletion cli/importrepotomoodle.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@
[
'longopt' => 'ignorecat',
'shortopt' => 'x',
'description' => 'Regex of categories to ignore',
'description' => 'Regex of categories to ignore - add an extra leading / for Windows.',
'default' => $ignorecat,
'variable' => 'ignorecat',
'valuerequired' => true,
Expand Down
2 changes: 1 addition & 1 deletion doc/createrepo.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
|n|instanceid|Numerical id of the course, module of course category.
|t|token|Security token for webservice.
|h|help|
|x|ignorecat|Regex of categories to ignore
|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows.

### Example 1:

Expand Down
2 changes: 1 addition & 1 deletion doc/deletefrommoodle.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
|n|instanceid|Numerical id of the course, module of course category.
|t|token|Security token for webservice.
|h|help|
|x|ignorecat|Regex of categories to ignore
|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows.

Examples:

Expand Down
2 changes: 1 addition & 1 deletion doc/exportrepofrommoodle.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
|q|questioncategoryid|Numerical id of subcategory to actually export.
|t|token|Security token for webservice.|
|h|help|
|x|ignorecat|Regex of categories to ignore
|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows.

Examples:

Expand Down
2 changes: 1 addition & 1 deletion doc/importrepotomoodle.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ Commit this update.
|n|instanceid|Numerical id of the course, module of course category.
|t|token|Security token for webservice.
|h|help|
|x|ignorecat|Regex of categories to ignore
|x|ignorecat|Regex of categories to ignore - add an extra leading / for Windows.

Examples:

Expand Down
2 changes: 1 addition & 1 deletion doc/usinggit.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ You can use context instance id and subcategory id instead:
* `-n` the context instance id, which is the course id in this case.
* `-q` the question category id

You can also ignore certain categories (and their descendants) using `-x` and a regex expression to match the category name(s) within Moodle.
You can also ignore certain categories (and their descendants) using `-x` and a regex expression to match the category name(s) within Moodle. (Add an extra leading / in Windows e.g. "//^.*DO_NOT_SHARE$/".)

`php createrepo.php -l course -n 2 -d "master" -q 80 -x "/^.*DO_NOT_SHARE$/"`

Expand Down
13 changes: 13 additions & 0 deletions tests/cli_helper_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,7 @@ public function test_validation_ignorecat(): void {
$helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '/hello/'];
$helper->validate_and_clean_args();
$this->expectOutputString('');
$this->assertEquals('/hello/', $helper->processedoptions['ignorecat']);
}

/**
Expand All @@ -516,4 +517,16 @@ public function test_validation_ignorecat_error(): void {
@$helper->validate_and_clean_args();
$this->expectOutputRegex('/problem with your regular expression/');
}

/**
* Validation
* @covers \gitsync\cli_helper\validate_and_clean_args()
*/
public function test_validation_ignorecat_replace(): void {
$helper = new fake_cli_helper([]);
$helper->processedoptions = ['token' => 'X', 'manifestpath' => 'path/subpath', 'ignorecat' => '//hello\//'];
$helper->validate_and_clean_args();
$this->expectOutputString('');
$this->assertEquals('/hello\//', $helper->processedoptions['ignorecat']);
}
}

0 comments on commit 886de84

Please sign in to comment.