diff --git a/.github/workflows/tests.yml b/.github/workflows/tests.yml index b973f12d8..a64cdaff0 100644 --- a/.github/workflows/tests.yml +++ b/.github/workflows/tests.yml @@ -3,34 +3,96 @@ name: Tests on: push: branches: - - 1.0 - - 1.1 - - 1.2 + - '1.2' - develop pull_request: jobs: frontendTests: - runs-on: ubuntu-latest - name: JavaScript + strategy: + max-parallel: 2 + matrix: + operatingSystem: [ubuntu-latest, windows-latest] + fail-fast: false + runs-on: ${{ matrix.operatingSystem }} + name: ${{ matrix.operatingSystem }} / JavaScript + env: + nodeVersion: 16 + phpVersion: '8.0' + extensions: curl, fileinfo, gd, mbstring, openssl, pdo, pdo_sqlite, sqlite3, xml, zip + key: winter-cms-cache-develop steps: - - name: Checkout changes - uses: actions/checkout@v2 + - name: Cancel previous incomplete runs + uses: styfle/cancel-workflow-action@0.8.0 with: - fetch-depth: 0 + access_token: ${{ github.token }} + + - name: Checkout changes + uses: actions/checkout@v3 + + - name: Setup extension cache + id: extcache + uses: shivammathur/cache-extensions@v1 + with: + php-version: ${{ env.phpVersion }} + extensions: ${{ env.extensions }} + key: ${{ env.key }} + + - name: Cache extensions + uses: actions/cache@v3 + with: + path: ${{ steps.extcache.outputs.dir }} + key: ${{ steps.extcache.outputs.key }} + restore-keys: ${{ steps.extcache.outputs.key }} + + - name: Install PHP + uses: shivammathur/setup-php@v2 + with: + php-version: ${{ env.phpVersion }} + extensions: ${{ env.extensions }} - name: Install Node - uses: actions/setup-node@v1 + uses: actions/setup-node@v3 with: - node-version: 12 + node-version: ${{ env.nodeVersion }} - - name: Install Node dependencies - working-directory: ./modules/system/tests/js - run: npm install + - name: Echo branches + run: echo "${{ github.ref }} | ${{ github.head_ref }} | ${{ github.ref_name }} | ${{ github.base_ref }}" + + - name: Switch library dependency (develop) + if: github.ref == 'refs/heads/develop' || github.base_ref == 'develop' + run: php ./.github/workflows/utilities/library-switcher "dev-develop as 1.2" + + - name: Switch library dependency (1.2) + if: github.head_ref == '1.2' || github.ref == 'refs/heads/1.2' || github.base_ref == '1.2' + run: php ./.github/workflows/utilities/library-switcher "1.2.x-dev as 1.2" + + - name: Setup dependency cache + id: composercache + run: echo "::set-output name=dir::$(composer config cache-files-dir)" + + - name: Cache dependencies + uses: actions/cache@v3 + with: + path: ${{ steps.composercache.outputs.dir }} + key: ${{ runner.os }}-composer-${{ hashFiles('**/composer.json') }} + restore-keys: ${{ runner.os }}-composer- + + - name: Install Composer dependencies + run: composer install --no-interaction --no-progress --no-scripts + + - name: Reset modules + run: | + git reset --hard + git clean -fd + + - name: Run post-update Composer scripts + run: php artisan package:discover - name: Run tests - working-directory: ./modules/system/tests/js - run: npm run test + run: | + php artisan mix:install + php artisan mix:run module-system test phpUnitTests: strategy: @@ -108,7 +170,9 @@ jobs: run: composer install --no-interaction --no-progress --no-scripts - name: Reset modules - run: git reset --hard + run: | + git reset --hard + git clean -fd - name: Run post-update Composer scripts run: php artisan package:discover diff --git a/modules/system/tests/js/.babelrc b/modules/system/.babelrc similarity index 89% rename from modules/system/tests/js/.babelrc rename to modules/system/.babelrc index 710cfd1b1..d6a9403c2 100644 --- a/modules/system/tests/js/.babelrc +++ b/modules/system/.babelrc @@ -6,7 +6,7 @@ "@babel/plugin-transform-runtime", [ "module-resolver", { - "root": ["."], + "root": ["./tests/js"], "alias": { "helpers": "./helpers", "snowboard": "../../modules/system/assets/js/snowboard", diff --git a/modules/system/ServiceProvider.php b/modules/system/ServiceProvider.php index e51ba5dd2..f7d3bfb6d 100644 --- a/modules/system/ServiceProvider.php +++ b/modules/system/ServiceProvider.php @@ -274,6 +274,7 @@ class ServiceProvider extends ModuleServiceProvider $this->registerConsoleCommand('mix.list', \System\Console\MixList::class); $this->registerConsoleCommand('mix.compile', \System\Console\MixCompile::class); $this->registerConsoleCommand('mix.watch', \System\Console\MixWatch::class); + $this->registerConsoleCommand('mix.run', \System\Console\MixRun::class); } /* diff --git a/modules/system/console/MixRun.php b/modules/system/console/MixRun.php new file mode 100644 index 000000000..424cea57e --- /dev/null +++ b/modules/system/console/MixRun.php @@ -0,0 +1,102 @@ +fireCallbacks(); + + $packages = $mixedAssets->getPackages(); + $name = $this->argument('package'); + $script = $this->argument('script'); + + if (!in_array($name, array_keys($packages))) { + $this->error( + sprintf('Package "%s" is not a registered package.', $name) + ); + return 1; + } + + $package = $packages[$name]; + $packageJson = $this->readPackageJson($package); + + if (!isset($packageJson['scripts'][$script])) { + $this->error( + sprintf('Script "%s" is not defined in package "%s".', $script, $name) + ); + return 1; + } + + $this->info(sprintf('Running script "%s" in package "%s"', $script, $name)); + + $command = ($this->argument('additionalArgs')) ?? []; + if (count($command)) { + array_unshift($command, 'npm', 'run', $script, '--'); + } else { + array_unshift($command, 'npm', 'run', $script); + } + + + $process = new Process( + $command, + base_path($package['path']), + ['NODE_ENV' => $this->option('production', false) ? 'production' : 'development'], + null, + null + ); + + try { + $process->setTty(true); + } catch (\Throwable $e) { + // This will fail on unsupported systems + } + + $exitCode = $process->run(function ($status, $stdout) { + if (!$this->option('silent')) { + $this->getOutput()->write($stdout); + } + }); + + return $exitCode; + } + + /** + * Reads the package.json file for the given package. + */ + protected function readPackageJson(array $package): array + { + $packageJsonPath = base_path($package['package']); + return File::exists($packageJsonPath) + ? json_decode(File::get($packageJsonPath), true) + : []; + } +} diff --git a/modules/system/package.json b/modules/system/package.json index 733390bb3..c136c0422 100644 --- a/modules/system/package.json +++ b/modules/system/package.json @@ -23,15 +23,25 @@ "url": "https://github.com/wintercms/winter/issues" }, "homepage": "https://wintercms.com/", + "scripts": { + "test": "jest --config tests/js/jest.config.js tests/js/cases" + }, "dependencies": { "js-cookie": "^3.0.1" }, "devDependencies": { + "@babel/core": "^7.16.0", + "@babel/plugin-transform-runtime": "^7.16.4", + "@babel/preset-env": "^7.16.4", + "@babel/register": "^7.16.0", + "@babel/runtime": "^7.16.3", "babel-plugin-module-resolver": "^4.1.0", "eslint": "^8.6.0", "eslint-config-airbnb-base": "^15.0.0", "eslint-plugin-import": "^2.25.4", "eslint-plugin-vue": "^8.5.0", + "jest": "^27.4.3", + "jsdom": "^18.1.1", "laravel-mix": "^6.0.34", "laravel-mix-polyfill": "^3.0.1" } diff --git a/modules/system/tests/js/.gitignore b/modules/system/tests/js/.gitignore deleted file mode 100644 index 7f9265d1a..000000000 --- a/modules/system/tests/js/.gitignore +++ /dev/null @@ -1,3 +0,0 @@ -# Ignore packages -package-lock.json -node_modules diff --git a/modules/system/tests/js/README.md b/modules/system/tests/js/README.md deleted file mode 100644 index 790325257..000000000 --- a/modules/system/tests/js/README.md +++ /dev/null @@ -1,32 +0,0 @@ -# Snowboard Framework Testing Suite - -The files in this directory form the testing suite for the new Winter JavaScript framework - Snowboard. You must install -all Node dependencies in order to run the testing suite: - -```bash -cd tests/js -npm install -``` - -You can then run the tests by using the following command: - -```bash -npm run test -``` - -Please note that the tests are run against the "built" versions of Snowboard and its plugins, in order to test -the exact same functionality that would be delivered to the end user. We do this by leveraging the `JSDOM` library to -simulate an entire HTML document. - -Therefore, you must compile a new build if you make any changes and wish to run the tests: - -```bash -php artisan mix:compile --package snowboard -``` - -You can also watch the framework for any changes, which will automatically run a build after a change is made. This will -make development and testing in parallel much quicker: - -```bash -php artisan mix:watch snowboard -``` diff --git a/modules/system/tests/js/package.json b/modules/system/tests/js/package.json deleted file mode 100644 index 650f38698..000000000 --- a/modules/system/tests/js/package.json +++ /dev/null @@ -1,37 +0,0 @@ -{ - "name": "@wintercms/tests", - "description": "Test cases for the Winter JavaScript framework", - "private": true, - "scripts": { - "test": "jest" - }, - "repository": { - "type": "git", - "url": "git+https://github.com/wintercms/winter.git" - }, - "contributors": [ - { - "name": "Ben Thomson", - "email": "git@alfreido.com" - }, - { - "name": "Winter CMS Maintainers", - "url": "https://wintercms.com/" - } - ], - "license": "MIT", - "bugs": { - "url": "https://github.com/wintercms/winter/issues" - }, - "homepage": "https://wintercms.com/", - "devDependencies": { - "@babel/core": "^7.16.0", - "@babel/plugin-transform-runtime": "^7.16.4", - "@babel/preset-env": "^7.16.4", - "@babel/register": "^7.16.0", - "@babel/runtime": "^7.16.3", - "babel-plugin-module-resolver": "^4.1.0", - "jest": "^27.4.3", - "jsdom": "^18.1.1" - } -}