Skip to content

AC-1156: Move custom eslint tests to magento-coding-standard repo #262

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

Merged
merged 2 commits into from
Sep 20, 2021
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
8 changes: 8 additions & 0 deletions .github/workflows/php.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ jobs:
name: Tests with PHP ${{ matrix.php-version }} and ${{ matrix.dependencies }} dependencies

steps:
- name: Setup node
uses: actions/setup-node@v2
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


- uses: actions/checkout@v2

- name: Setup PHP
Expand All @@ -34,6 +37,11 @@ jobs:
env:
COMPOSER_TOKEN: ${{ secrets.GITHUB_TOKEN }}

- name: Install dependencies
run: npm install
- name: Run ESLint
run: npm run eslint -- eslint/rules
Comment on lines +40 to +43
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be separated into a different workflow to run the tests on different nodejs versions, also I don't think we should run eslint tests on different PHP versions


- name: Validate composer
run: composer validate

Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/vendor/*
/bin/*
/node_modules

# IDE files
.idea/
Expand Down
34 changes: 34 additions & 0 deletions Magento2/Tests/Eslint/AbstractEslintTestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

use PHPUnit\Framework\TestCase;

/**
* Abstract class AbstractEslintTestCase
*
* Test Eslint Rules (magento-coding-standard/eslint/rules)
*/
abstract class AbstractEslintTestCase extends TestCase
{
/**
* @param string $testFile
* @param array $expectedMessages
*/
protected function assertFileContainsError(string $testFile, array $expectedMessages): void
{
exec(
'npm run eslint -- Magento2/Tests/Eslint/' . $testFile,
$output
);

foreach ($expectedMessages as $message) {
$this->assertStringContainsString($message, implode(' ',$output));
}
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/AndSelfTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function() {
'use strict';
$("div").find("p").andSelf().addClass("border");
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/AndSelfTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class AndSelfTest
*
* Test Eslint Rule: jquery-no-andSelf.js
*/
class AndSelfTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'AndSelfTest.js',
['jQuery.andSelf() removed, use jQuery.addBack()']
);
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/BindUnbindTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function(){
'use strict';
$(".btn1").bind("click");
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/BindUnbindTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class BindUnbindTest
*
* Test Eslint Rule: jquery-no-bind-unbind.js
*/
class BindUnbindTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'BindUnbindTest.js',
['jQuery $.bind and $.unbind are deprecated, use $.on and $.off instead']
);
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/ClickEventShorthand.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function(){
'use strict';
$("input").blur();
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/ClickEventShorthandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class ClickEventShorthandTest
*
* Test Eslint Rule: jquery-no-click-event-shorthand.js
*/
class ClickEventShorthandTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'ClickEventShorthand.js',
['Instead of .blur(fn) use .on("blur", fn). Instead of .blur() use .trigger("blur")']
);
}
}
6 changes: 6 additions & 0 deletions Magento2/Tests/Eslint/DelegateUndelegateTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
$(document).ready(function(){
'use strict';
$( "table" ).delegate( "td", "click", function() {
$( this ).toggleClass( "chosen" );
});
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/DelegateUndelegateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class DelegateUndelegateTest
*
* Test Eslint Rule: jquery-no-delegate-undelegate.js
*/
class DelegateUndelegateTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'DelegateUndelegateTest.js',
['jQuery $.delegate and $.undelegate are deprecated, use $.on and $.off instead']
);
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/EventShorthandTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function(){
'use strict';
$( "#result" ).load( "ajax/test.html" );
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/EventShorthandTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class EventShorthandTest
*
* Test Eslint Rule: jquery-no-event-shorthand.js
*/
class EventShorthandTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'EventShorthandTest.js',
['jQuery.load() was removed, use .on("load", fn) instead']
);
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/SizeTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function(){
'use strict';
$("div").size();
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/SizeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class SizeTest
*
* Test Eslint Rule: jquery-no-size.js
*/
class SizeTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'SizeTest.js',
['jQuery.size() removed, use jQuery.length']
);
}
}
4 changes: 4 additions & 0 deletions Magento2/Tests/Eslint/TrimTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$(document).ready(function(){
'use strict';
$.trim(" hello, how are you? ");
});
24 changes: 24 additions & 0 deletions Magento2/Tests/Eslint/TrimTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento2\Tests\Eslint;

/**
* Class TrimTest
*
* Test Eslint Rule: jquery-no-trim.js
*/
class TrimTest extends AbstractEslintTestCase
{
public function testExecute(): void
{
$this->assertFileContainsError(
'TrimTest.js',
['jQuery.trim is deprecated; use String.prototype.trim']
);
}
}
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,18 @@ Also, verify that the sniffer code itself is written according to the Magento Co
vendor/bin/phpcs --standard=Magento2 Magento2/ --extensions=php
```

### ESLint testing
Prerequisites: [Node.js](https://nodejs.org/) (`^12.22.0`, `^14.17.0`, or `>=16.0.0`).

You need to run the following command to install all the necessary packages described in the `package.json` file:
```bash
npm install
```

After that, you can just execute ESLint as follows:
```bash
npm run eslint -- path/to/analyze
```
## License

Each Magento source file included in this distribution is licensed under the OSL-3.0 license.
Expand Down
8 changes: 8 additions & 0 deletions eslint/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": [
"./.eslintrc-reset",
"./.eslintrc-magento",
"./.eslintrc-jquery",
"./.eslintrc-misc"
]
}
11 changes: 11 additions & 0 deletions eslint/.eslintrc-jquery
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"rules": {
"jquery-no-andSelf": 2,
"jquery-no-bind-unbind": 2,
"jquery-no-click-event-shorthand": 2,
"jquery-no-delegate-undelegate": 2,
"jquery-no-event-shorthand": 2,
"jquery-no-size": 2,
"jquery-no-trim": 2
}
}
Loading