Skip to content

Commit

Permalink
test: run tests in complete isolation
Browse files Browse the repository at this point in the history
  • Loading branch information
rainerhahnekamp committed Aug 19, 2024
1 parent aba8027 commit 9f97fee
Show file tree
Hide file tree
Showing 9 changed files with 623 additions and 65 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,4 @@ Thumbs.db
.nx
.yalc
yalc.lock
.test-projects
27 changes: 18 additions & 9 deletions run-integration-tests.sh
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
set -e

# This is necessary because @angular-eslint uses nx and would
# use root as workspace... ending up in strange error messages.
# This can go away as soon as angular-eslint switches to flat
# config.
# We copy the test projects to a temporary directory to avoid any potential
# issues with the dependencies from the root project.

mv nx.json nx.json.bak
mv node_modules node_modules.bak
# Check if .test-projects exists and is a symbolic link
if [ -L .test-projects ]; then
TARGET_DIR=$(readlink .test-projects)
rm .test-projects
rm -rf "$TARGET_DIR"
echo "Removing $TARGET_DIR"
fi

export TMP_DIR=$(mktemp -d)
rsync -a --exclude node_modules --exclude .angular test-projects/ "$TMP_DIR"
ln -sf "$TMP_DIR" .test-projects

echo "Temporary directory created at $TMP_DIR"

echo "Testing against Angular 15 (ESLint Legacy)"
cd test-projects/angular-i
cd .test-projects/angular-i
bash ./integration-test.sh

echo "Testing against Angular 18 (ESLint Flat)"
Expand All @@ -20,5 +29,5 @@ cd ../typescript-i
bash ./integration-test.sh

cd ../..
mv nx.json.bak nx.json
mv node_modules.bak node_modules

echo "Tests finished successfully"
13 changes: 8 additions & 5 deletions test-projects/angular-i/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ yalc add @softarc/sheriff-core @softarc/eslint-plugin-sheriff
cd node_modules/.bin # yalc doesn't create symlink in node_modules/.bin
ln -s ../@softarc/sheriff-core/src/bin/main.js ./sheriff
cd ../../
cp sheriff.config.ts sheriff.config.ts.original

# CLI List Check
echo 'checking for CLI list'
Expand All @@ -26,36 +27,38 @@ cp tests/dynamic-import-sheriff.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/dynamic-import-lint.json
../remove-paths.mjs tests/actual/dynamic-import-lint.json
diff tests/actual/dynamic-import-lint.json tests/expected/dynamic-import-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts

## Deep Import Check
echo 'checking for deep import error'
mv src/app/customers/feature/components/customers-container.component.ts src/app/customers/feature/components/customers-container.component.ts.original
cp tests/customers-container.deep-import.component.ts src/app/customers/feature/components/customers-container.component.ts
npx ng lint --force --format json --output-file tests/actual/deep-import-lint.json
../remove-paths.mjs tests/actual/deep-import-lint.json
diff tests/actual/deep-import-lint.json tests/expected/deep-import-lint.json
git checkout -q src/app/customers/feature/components/customers-container.component.ts
mv src/app/customers/feature/components/customers-container.component.ts.original src/app/customers/feature/components/customers-container.component.ts

## Dependency Rule Check
echo 'checking for dependency rule error'
mv src/app/customers/ui/customer/customer.component.ts src/app/customers/ui/customer/customer.component.ts.original
cp tests/customer.dependency-rule.component.ts src/app/customers/ui/customer/customer.component.ts
npx ng lint --force --format json --output-file tests/actual/dependency-rule-lint.json
../remove-paths.mjs tests/actual/dependency-rule-lint.json
diff tests/actual/dependency-rule-lint.json tests/expected/dependency-rule-lint.json
git checkout -q src/app/customers/ui/customer/customer.component.ts
mv src/app/customers/ui/customer/customer.component.ts.original src/app/customers/ui/customer/customer.component.ts

## User Error Processing
echo 'checking for user error processing'
cp tests/empty-sheriff.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/user-error-lint.json
../remove-paths.mjs tests/actual/user-error-lint.json
diff tests/actual/user-error-lint.json tests/expected/user-error-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts

## Auto Tagging
echo 'checking for auto tagging'
cp tests/auto-tagging.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/auto-tagging-lint.json
../remove-paths.mjs tests/actual/auto-tagging-lint.json
diff tests/actual/auto-tagging-lint.json tests/expected/auto-tagging-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts
4 changes: 2 additions & 2 deletions test-projects/angular-i/tests/expected/deep-import-lint.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"column": 1,
"nodeType": "ImportDeclaration",
"endLine": 4,
"endColumn": 82
"endColumn": 79
}
],
"suppressedMessages": [],
Expand All @@ -19,7 +19,7 @@
"warningCount": 0,
"fixableErrorCount": 0,
"fixableWarningCount": 0,
"source": "import { AsyncPipe, NgIf } from '@angular/common';\nimport { Component, inject } from '@angular/core';\nimport { CustomersComponent, CustomersViewModel } from '@eternal/customers/ui';\nimport { CustomersRepository } from '../../data/customers-repository.service.ts';\nimport { Observable } from 'rxjs';\nimport { map } from 'rxjs/operators';\n\n@Component({\n template: ` <eternal-customers\n *ngIf=\"viewModel$ | async as viewModel\"\n [viewModel]=\"viewModel\"\n (setSelected)=\"setSelected($event)\"\n (setUnselected)=\"setUnselected()\"\n (switchPage)=\"switchPage($event)\"\n ></eternal-customers>`,\n standalone: true,\n imports: [CustomersComponent, NgIf, AsyncPipe],\n})\nexport class CustomersContainerComponent {\n #customersRepository = inject(CustomersRepository);\n viewModel$: Observable<CustomersViewModel> =\n this.#customersRepository.pagedCustomers$.pipe(\n map((pagedCustomers) => ({\n customers: pagedCustomers.customers,\n pageIndex: pagedCustomers.page - 1,\n length: pagedCustomers.total,\n }))\n );\n\n setSelected(id: number) {\n this.#customersRepository.select(id);\n }\n\n setUnselected() {\n this.#customersRepository.unselect();\n }\n\n switchPage(page: number) {\n console.log('switch to page ' + page + ' is not implemented');\n }\n}\n",
"source": "import { AsyncPipe, NgIf } from '@angular/common';\nimport { Component, inject } from '@angular/core';\nimport { CustomersComponent, CustomersViewModel } from '@eternal/customers/ui';\nimport { CustomersRepository } from '../../data/customers-repository.service';\nimport { Observable } from 'rxjs';\nimport { map } from 'rxjs/operators';\n\n@Component({\n template: ` <eternal-customers\n *ngIf=\"viewModel$ | async as viewModel\"\n [viewModel]=\"viewModel\"\n (setSelected)=\"setSelected($event)\"\n (setUnselected)=\"setUnselected()\"\n (switchPage)=\"switchPage($event)\"\n ></eternal-customers>`,\n standalone: true,\n imports: [CustomersComponent, NgIf, AsyncPipe],\n})\nexport class CustomersContainerComponent {\n #customersRepository = inject(CustomersRepository);\n viewModel$: Observable<CustomersViewModel> =\n this.#customersRepository.pagedCustomers$.pipe(\n map((pagedCustomers) => ({\n customers: pagedCustomers.customers,\n pageIndex: pagedCustomers.page - 1,\n length: pagedCustomers.total,\n }))\n );\n\n setSelected(id: number) {\n this.#customersRepository.select(id);\n }\n\n setUnselected() {\n this.#customersRepository.unselect();\n }\n\n switchPage(page: number) {\n console.log('switch to page ' + page + ' is not implemented');\n }\n}\n",
"usedDeprecatedRules": []
}
]
23 changes: 15 additions & 8 deletions test-projects/angular-iv/integration-test.sh
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
set -e
yarn
yalc add @softarc/sheriff-core @softarc/eslint-plugin-sheriff
cd node_modules/.bin # yalc doesn't create symlink in node_modules/.bin
ln -s ../@softarc/sheriff-core/src/bin/main.js ./sheriff
cd ../../
cp sheriff.config.ts sheriff.config.ts.original

# CLI List Check
echo 'checking for CLI list'
sheriff list src/main.ts > tests/actual/cli-list.txt
npx sheriff list src/main.ts > tests/actual/cli-list.txt
diff tests/actual/cli-list.txt tests/expected/cli-list.txt

# CLI Export Check
echo 'checking for CLI export'
sheriff export src/main.ts > tests/actual/cli-export.txt
npx sheriff export src/main.ts > tests/actual/cli-export.txt
diff tests/actual/cli-export.txt tests/expected/cli-export.txt

# CLI Verify Check
echo 'checking for CLI verify'
sheriff verify src/main.ts > tests/actual/cli-verify.txt
npx sheriff verify src/main.ts > tests/actual/cli-verify.txt
diff tests/actual/cli-verify.txt tests/expected/cli-verify.txt

# Dynamic Import Check
Expand All @@ -22,36 +27,38 @@ cp tests/dynamic-import-sheriff.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/dynamic-import-lint.json
../remove-paths.mjs tests/actual/dynamic-import-lint.json
diff tests/actual/dynamic-import-lint.json tests/expected/dynamic-import-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts

## Deep Import Check
echo 'checking for deep import error'
mv src/app/customers/feature/components/customers-container.component.ts src/app/customers/feature/components/customers-container.component.ts.original
cp tests/customers-container.deep-import.component.ts src/app/customers/feature/components/customers-container.component.ts
npx ng lint --force --format json --output-file tests/actual/deep-import-lint.json
../remove-paths.mjs tests/actual/deep-import-lint.json
diff tests/actual/deep-import-lint.json tests/expected/deep-import-lint.json
git checkout -q src/app/customers/feature/components/customers-container.component.ts
mv src/app/customers/feature/components/customers-container.component.ts.original src/app/customers/feature/components/customers-container.component.ts

## Dependency Rule Check
echo 'checking for dependency rule error'
mv src/app/customers/ui/customer/customer.component.ts src/app/customers/ui/customer/customer.component.ts.original
cp tests/customer.dependency-rule.component.ts src/app/customers/ui/customer/customer.component.ts
npx ng lint --force --format json --output-file tests/actual/dependency-rule-lint.json
../remove-paths.mjs tests/actual/dependency-rule-lint.json
diff tests/actual/dependency-rule-lint.json tests/expected/dependency-rule-lint.json
git checkout -q src/app/customers/ui/customer/customer.component.ts
mv src/app/customers/ui/customer/customer.component.ts.original src/app/customers/ui/customer/customer.component.ts

## User Error Processing
echo 'checking for user error processing'
cp tests/empty-sheriff.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/user-error-lint.json
../remove-paths.mjs tests/actual/user-error-lint.json
diff tests/actual/user-error-lint.json tests/expected/user-error-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts

## Auto Tagging
echo 'checking for auto tagging'
cp tests/auto-tagging.config.ts sheriff.config.ts
npx ng lint --force --format json --output-file tests/actual/auto-tagging-lint.json
../remove-paths.mjs tests/actual/auto-tagging-lint.json
diff tests/actual/auto-tagging-lint.json tests/expected/auto-tagging-lint.json
git checkout -q sheriff.config.ts
cp sheriff.config.ts.original sheriff.config.ts
6 changes: 5 additions & 1 deletion test-projects/remove-paths.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import * as fs from "fs";

const file = process.argv[2];

const tmpDir = process.env['TMP_DIR']
const lastSegment = tmpDir.split('/').pop()

let content = fs.readFileSync(file, {
encoding: "utf-8"
});
const cleanedContent = content.replace(/"[^"]+\/test-projects\/[^\/]+/g, '".')
const regExp = new RegExp(`"[^"]+/${lastSegment}/[^/]+`, 'g')
const cleanedContent = content.replace(regExp, '".')
const formattedContent = JSON.stringify(JSON.parse(cleanedContent), null, 2);
fs.writeFileSync(file, formattedContent, { encoding: "utf-8" });
7 changes: 6 additions & 1 deletion test-projects/typescript-i/integration-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ echo 'checking against different TypeScript versions'
declare -a versions=('4.8' '4.9' '5.0' '5.1' '5.2' '5.3' '5.4' '5.5')
declare -a configs=('.eslintrc.json' 'eslint.config.js')

npm i
yalc add @softarc/sheriff-core @softarc/eslint-plugin-sheriff
cd node_modules/.bin # yalc doesn't create symlink in node_modules/.bin
ln -s ../@softarc/sheriff-core/src/bin/main.js ./sheriff
cd ../../

for version in ${versions[*]}; do
for config in ${configs[*]}; do

Expand All @@ -25,7 +31,6 @@ for version in ${versions[*]}; do

echo "Testing with TypeScript $version, ESLint $eslint"
npm install typescript@$version
cp -r ../../node_modules/@softarc node_modules/
installed_version=$(npx tsc -v)

if [[ ! $installed_version == "Version $version"* ]]
Expand Down
Loading

0 comments on commit 9f97fee

Please sign in to comment.