diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..7dd8c24 --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,46 @@ +name: Tests + +on: [push, pull_request] + +env: + CI: true + +jobs: + run: + name: Node ${{ matrix.node }} on ${{ matrix.os }} + runs-on: ${{ matrix.os }} + + strategy: + fail-fast: false + matrix: + node: [14, 16] + os: [ubuntu-latest] + + steps: + - name: Clone repository + uses: actions/checkout@v2 + + - name: Set up Node.js + uses: actions/setup-node@v2 + with: + node-version: ${{ matrix.node }} + + - name: Cache Node Modules + id: cache-node-modules + uses: actions/cache@v2 + with: + path: node_modules + key: node-modules-${{ hashFiles('package-lock.json') }} + + - name: Install dependencies + run: npm ci + + - name: Run tests + run: npm test + + - name: SonarCloud Scan + uses: SonarSource/sonarcloud-github-action@master + if: startsWith(matrix.os, 'ubuntu') && startsWith(matrix.node, '16') + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} diff --git a/.gitignore b/.gitignore index 6a3a54b..7580e7d 100644 --- a/.gitignore +++ b/.gitignore @@ -20,6 +20,7 @@ pids lib-cov # Coverage directory used by tools like istanbul +__coverage__ coverage *.lcov diff --git a/index.js b/index.js index 88bc853..9689dc1 100644 --- a/index.js +++ b/index.js @@ -11,16 +11,16 @@ * }); * */ - const { getValidationHelpers } = require('./lib/helpers/validation'); - const { getLocaleHelpers } = require('./lib/helpers/locale'); +const { getValidationHelpers } = require('./lib/helpers/validation'); +const { getLocaleHelpers } = require('./lib/helpers/locale'); - // Export wrapper - module.exports.withHelpers = (cb) => (migration, context) => { - const locale = getLocaleHelpers(migration, context); - const validation = getValidationHelpers(migration, context); +// Export wrapper +module.exports.withHelpers = (cb) => (migration, context) => { + const locale = getLocaleHelpers(migration, context); + const validation = getValidationHelpers(migration, context); - return cb(migration, context, { locale, validation }); - }; + return cb(migration, context, { locale, validation }); +}; - module.exports.getValidationHelpers = getValidationHelpers; - module.exports.getLocaleHelpers = getLocaleHelpers; +module.exports.getValidationHelpers = getValidationHelpers; +module.exports.getLocaleHelpers = getLocaleHelpers; diff --git a/lib/migration.js b/lib/migration.js index 285d694..7667bf0 100644 --- a/lib/migration.js +++ b/lib/migration.js @@ -93,10 +93,12 @@ const fetchMigration = async (config) => { .toString() // Add call to utils.getDefaultLocale() to the top .replace( - 'module.exports = function (migration) {',` + 'module.exports = function (migration) {', + ` ${migrationHeader} const defaultLocale = helpers.locale.getDefaultLocale(); - `) + ` + ) // Replace the default locale with defaultLocale.code so that the migration // still works as expected when the locale is changed in contentful .replace(testDefaultValueRegex, '$1[defaultLocale.code]$2') diff --git a/package-lock.json b/package-lock.json index b03b315..55374f8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -47,6 +47,7 @@ "eslint-plugin-prettier": "^4.0.0", "husky": "^7.0.4", "jest": "^27.5.1", + "jest-sonar-reporter": "^2.0.0", "lint-staged": "^12.1.2" }, "engines": { @@ -6050,6 +6051,18 @@ "node": "^10.13.0 || ^12.13.0 || ^14.15.0 || >=15.0.0" } }, + "node_modules/jest-sonar-reporter": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/jest-sonar-reporter/-/jest-sonar-reporter-2.0.0.tgz", + "integrity": "sha512-ZervDCgEX5gdUbdtWsjdipLN3bKJwpxbvhkYNXTAYvAckCihobSLr9OT/IuyNIRT1EZMDDwR6DroWtrq+IL64w==", + "dev": true, + "dependencies": { + "xml": "^1.0.1" + }, + "engines": { + "node": ">=8.0.0" + } + }, "node_modules/jest-util": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", @@ -9925,6 +9938,12 @@ } } }, + "node_modules/xml": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz", + "integrity": "sha1-eLpyAgApxbyHuKgaPPzXS0ovweU=", + "dev": true + }, "node_modules/xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", @@ -14741,6 +14760,15 @@ "semver": "^7.3.2" } }, + "jest-sonar-reporter": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/jest-sonar-reporter/-/jest-sonar-reporter-2.0.0.tgz", + "integrity": "sha512-ZervDCgEX5gdUbdtWsjdipLN3bKJwpxbvhkYNXTAYvAckCihobSLr9OT/IuyNIRT1EZMDDwR6DroWtrq+IL64w==", + "dev": true, + "requires": { + "xml": "^1.0.1" + } + }, "jest-util": { "version": "27.5.1", "resolved": "https://registry.npmjs.org/jest-util/-/jest-util-27.5.1.tgz", @@ -17593,6 +17621,12 @@ "dev": true, "requires": {} }, + "xml": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/xml/-/xml-1.0.1.tgz", + "integrity": "sha1-eLpyAgApxbyHuKgaPPzXS0ovweU=", + "dev": true + }, "xml-name-validator": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/xml-name-validator/-/xml-name-validator-3.0.0.tgz", diff --git a/package.json b/package.json index 2e6e565..c8ddc5f 100644 --- a/package.json +++ b/package.json @@ -15,7 +15,7 @@ "migrations": "cli.js" }, "scripts": { - "test": "jest && npm run lint", + "test": "jest --coverage && npm run lint", "lint": "eslint . --ext .js,.ts,.tsx --ignore-path .gitignore", "fix-lint": "npm run lint -- --fix" }, @@ -68,6 +68,7 @@ "eslint-plugin-prettier": "^4.0.0", "husky": "^7.0.4", "jest": "^27.5.1", + "jest-sonar-reporter": "^2.0.0", "lint-staged": "^12.1.2" }, "publishConfig": { @@ -78,6 +79,9 @@ "printWidth": 120 }, "eslintConfig": { + "ignorePatterns": [ + "**/*.d.ts" + ], "plugins": [ "prettier" ], @@ -110,24 +114,11 @@ ] }, "jest": { + "testResultsProcessor": "jest-sonar-reporter", "coverageDirectory": "/__coverage__/", "collectCoverageFrom": [ "/lib/*/**" ], - "coverageThreshold": { - "global": { - "statements": 85, - "branches": 80, - "functions": 80, - "lines": 85 - }, - "**/*": { - "statements": 75, - "branches": 70, - "functions": 70, - "lines": 75 - } - }, "roots": [ "/lib/" ], @@ -154,5 +145,10 @@ "storage": "content", "migrationContentTypeId": "contentful-migrations", "directory": "migrations" + }, + "jestSonar": { + "reportPath": "__coverage__", + "reportFile": "test-report.xml", + "indent": 2 } } diff --git a/sonar-project.properties b/sonar-project.properties new file mode 100644 index 0000000..69d3243 --- /dev/null +++ b/sonar-project.properties @@ -0,0 +1,15 @@ +sonar.projectKey=jungvonmatt_contentful-migrations +sonar.organization=jungvonmatt + +# This is the name and version displayed in the SonarCloud UI. +#sonar.projectName=contentful-migrations +#sonar.projectVersion=1.0 + +# Path is relative to the sonar-project.properties file. Replace "\" by "/" on Windows. +#sonar.sources=. + +# Encoding of the source code. Default is default system encoding +#sonar.sourceEncoding=UTF-8 + +sonar.testExecutionReportPaths=__coverage__/test-report.xml +sonar.javascript.lcov.reportPaths=__coverage__/lcov.info