Skip to content

Commit

Permalink
fix: repair cleanup testbed script and apply changes (#1057)
Browse files Browse the repository at this point in the history
* fix: repair cleanup-testbed script
* chore: add cleanup-testbed to npm scripts
* chore: run cleanup-testbed
  • Loading branch information
dhhyi authored Mar 15, 2022
1 parent 7aee442 commit a4e403c
Show file tree
Hide file tree
Showing 57 changed files with 136 additions and 343 deletions.
4 changes: 1 addition & 3 deletions .github/workflows/dead-code.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,4 @@ jobs:
run: npm ci

- name: Find Unused TestBed declarations
run: |
npx ts-node scripts/cleanup-testbed
npm run check-no-changes
run: npx npm-run-all cleanup-testbed check-no-changes
4 changes: 1 addition & 3 deletions .github/workflows/development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,7 @@ jobs:
run: npx ts-node scripts/find-dead-code

- name: Find Unused TestBed declarations
run: |
npx ts-node scripts/cleanup-testbed origin/develop
npm run check-no-changes
run: npx npm-run-all "cleanup-testbed origin/develop" check-no-changes

Schematics:
needs: [Quality, Jest]
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"compile": "tsc --project tsconfig.all.json",
"dead-code": "npx ts-node scripts/find-dead-code.ts",
"clean-localizations": "node scripts/clean-up-localizations",
"cleanup-testbed": "npx ts-node scripts/cleanup-testbed",
"spell-check": "cspell lint --no-summary --no-progress \"{src,projects,docs}/**/*.{html,ts,scss,md}\"",
"spell-check-localizations": "cspell lint --no-summary --no-progress \"src/assets/i18n/{de_DE,en_US,fr_FR}.json\"",
"clean": "git clean -xdf -e \"*environment.local.ts\" -e \"*environment.development.ts\" -e \"*node_modules\"",
Expand Down
65 changes: 30 additions & 35 deletions scripts/cleanup-testbed.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { tsquery } from '@phenomnomnominal/tsquery';
import { execSync, spawnSync } from 'child_process';
import { LeftHandSideExpression, Node, Project, PropertyAccessExpression, SourceFile } from 'ts-morph';
import * as ts from 'typescript';

/* eslint-disable no-console */

Expand Down Expand Up @@ -38,44 +36,44 @@ console.log(`found ${files.length} test file(s)`);

let processedFiles = 0;

const lintFiles = [];

for (const file of files) {
processedFiles++;

const hasTestBedModule = file.getText().includes('configureTestingModule');

const copyPath = `${file.getFilePath()}.ut.spec.ts`;
let foundSomething = false;

top: while (true) {
const percent = ((processedFiles / files.length) * 100).toFixed(0);

if (
!tsquery(
file.getSourceFile().compilerNode as unknown as ts.Node,
'PropertyAccessExpression[expression.text=TestBed][name.text=configureTestingModule]'
).length
) {
if (!hasTestBedModule) {
console.log(`at ${percent}% - ${path(file)}`, 0, `test(s)`);
} else {
const configs: { tb: number; type: string; index: number }[] = [];

let tb = 0;
file.forEachDescendant(node => {
if (Node.isCallExpression(node) && isTestBedConfigure(node.getExpression())) {
const configObject = node.getArguments().find(Node.isObjectLiteralExpression);
if (configObject) {
for (const type of ['declarations', 'providers', 'imports']) {
const array = configObject.getProperty(type);
if (Node.isPropertyAssignment(array)) {
const initializer = array.getInitializer();
if (Node.isArrayLiteralExpression(initializer)) {
for (let index = 0; index < initializer.getElements().length; index++) {
configs.push({ tb, type, index });
project.forgetNodesCreatedInBlock(() => {
file.forEachDescendant(node => {
if (Node.isCallExpression(node) && isTestBedConfigure(node.getExpression())) {
const configObject = node.getArguments().find(Node.isObjectLiteralExpression);
if (configObject) {
for (const type of ['declarations', 'providers', 'imports']) {
const array = configObject.getProperty(type);
if (Node.isPropertyAssignment(array)) {
const initializer = array.getInitializer();
if (Node.isArrayLiteralExpression(initializer)) {
for (let index = initializer.getElements().length - 1; index >= 0; index--) {
configs.push({ tb, type, index });
}
}
}
}
}
tb++;
}
tb++;
}
});
});

console.log(`at ${percent}% - ${path(file)}`, configs.length, `test(s)`);
Expand All @@ -94,7 +92,7 @@ for (const file of files) {
if (Node.isPropertyAssignment(array)) {
const initializer = array.getInitializer();
if (Node.isArrayLiteralExpression(initializer)) {
const element = initializer.getElements()[config.index].getText();
const element = initializer.getElements()[config.index]?.getText();

if (initializer.getElements().length === 1) {
array.remove();
Expand All @@ -113,7 +111,10 @@ for (const file of files) {
`${path(file)} - removing '${element}' from ${config.type} in TestBed #${config.tb + 1}`
);
copy.copyImmediatelySync(file.getFilePath(), { overwrite: true });
foundSomething = true;
lintFiles.push(file.getFilePath());
copy.forget();
copy.deleteImmediatelySync();

continue top;
} catch (err) {
continue next;
Expand All @@ -130,18 +131,12 @@ for (const file of files) {
}

if (project.getSourceFile(copyPath)) {
project.getSourceFile(copyPath).delete();
}

if (foundSomething) {
const filePath = file.getFilePath();
execSync(`node scripts/fix-imports ${filePath}`);
try {
execSync(`npx prettier --write ${filePath}`);
} catch (err) {
// do nothing
}
project.getSourceFile(copyPath).deleteImmediatelySync();
}
}

project.saveSync();

if (lintFiles.length) {
execSync(`npx eslint --fix ${lintFiles.join(' ')}`);
}
11 changes: 1 addition & 10 deletions src/app/core/guards/auth.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { ActivatedRouteSnapshot, RouterStateSnapshot, UrlTree } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand All @@ -19,16 +18,8 @@ describe('Auth Guard', () => {
let store$: Store;

beforeEach(async () => {
@Component({ template: 'dummy' })
class DummyComponent {}

await TestBed.configureTestingModule({
imports: [
CoreStoreModule.forTesting(),
CustomerStoreModule.forTesting('user'),
RouterTestingModule.withRoutes([{ path: 'login', component: DummyComponent }]),
],
declarations: [DummyComponent],
imports: [CoreStoreModule.forTesting(), CustomerStoreModule.forTesting('user'), RouterTestingModule],
providers: [{ provide: CookiesService, useFactory: () => instance(mock(CookiesService)) }],
}).compileComponents();
});
Expand Down
11 changes: 3 additions & 8 deletions src/app/core/guards/authorization-toggle.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand All @@ -11,27 +10,23 @@ describe('Authorization Toggle Guard', () => {
let router: Router;

beforeEach(() => {
@Component({ template: 'dummy' })
class DummyComponent {}

TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
AuthorizationToggleModule.forTesting('DO_THIS'),
RouterTestingModule.withRoutes([
{
path: 'error',
component: DummyComponent,
children: [],
},
{
path: 'page1',
component: DummyComponent,
children: [],
canActivate: [AuthorizationToggleGuard],
data: { permission: 'DO_THIS' },
},
{
path: 'page2',
component: DummyComponent,
children: [],
canActivate: [AuthorizationToggleGuard],
data: { permission: 'DO_THAT' },
},
Expand Down
11 changes: 3 additions & 8 deletions src/app/core/guards/feature-toggle.guard.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { ChangeDetectionStrategy, Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand All @@ -9,27 +8,23 @@ describe('Feature Toggle Guard', () => {
let router: Router;

beforeEach(() => {
@Component({ template: 'dummy', changeDetection: ChangeDetectionStrategy.OnPush })
class DummyComponent {}

TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
FeatureToggleModule.forTesting('feature1'),
RouterTestingModule.withRoutes([
{
path: 'error',
component: DummyComponent,
children: [],
},
{
path: 'feature1',
component: DummyComponent,
children: [],
canActivate: [FeatureToggleGuard],
data: { feature: 'feature1' },
},
{
path: 'feature2',
component: DummyComponent,
children: [],
canActivate: [FeatureToggleGuard],
data: { feature: 'feature2' },
},
Expand Down
13 changes: 4 additions & 9 deletions src/app/core/identity-provider/auth0.identity-provider.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { APP_BASE_HREF } from '@angular/common';
import { Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Router } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand All @@ -17,9 +16,6 @@ import { ApiTokenService } from 'ish-core/utils/api-token/api-token.service';

import { Auth0Config, Auth0IdentityProvider } from './auth0.identity-provider';

@Component({ template: 'dummy' })
class DummyComponent {}

const idToken = 'abc123';

const userData: UserData = {
Expand Down Expand Up @@ -47,13 +43,12 @@ describe('Auth0 Identity Provider', () => {

beforeEach(() => {
TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
RouterTestingModule.withRoutes([
{ path: 'register', component: DummyComponent },
{ path: 'register/sso', component: DummyComponent },
{ path: 'account', component: DummyComponent },
{ path: 'logout', component: DummyComponent },
{ path: 'register', children: [] },
{ path: 'register/sso', children: [] },
{ path: 'account', children: [] },
{ path: 'logout', children: [] },
]),
],
providers: [
Expand Down
10 changes: 1 addition & 9 deletions src/app/core/routing/category/category.route.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Router, UrlMatchResult, UrlSegment } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand Down Expand Up @@ -166,15 +165,8 @@ describe('Category Route', () => {
let store$: Store;

beforeEach(() => {
@Component({ template: 'dummy' })
class DummyComponent {}

TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
CoreStoreModule.forTesting(['router']),
RouterTestingModule.withRoutes([{ path: '**', component: DummyComponent }]),
],
imports: [CoreStoreModule.forTesting(['router']), RouterTestingModule.withRoutes([{ path: '**', children: [] }])],
});

router = TestBed.inject(Router);
Expand Down
10 changes: 1 addition & 9 deletions src/app/core/routing/product/product.route.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from '@angular/core';
import { TestBed, fakeAsync, tick } from '@angular/core/testing';
import { Router, UrlMatchResult, UrlSegment } from '@angular/router';
import { RouterTestingModule } from '@angular/router/testing';
Expand Down Expand Up @@ -338,15 +337,8 @@ describe('Product Route', () => {
let store$: Store;

beforeEach(() => {
@Component({ template: 'dummy' })
class DummyComponent {}

TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
CoreStoreModule.forTesting(['router']),
RouterTestingModule.withRoutes([{ path: '**', component: DummyComponent }]),
],
imports: [CoreStoreModule.forTesting(['router']), RouterTestingModule.withRoutes([{ path: '**', children: [] }])],
});

router = TestBed.inject(Router);
Expand Down
7 changes: 1 addition & 6 deletions src/app/core/services/api/api.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ import {
import { CoreStoreModule } from 'ish-core/store/core/core-store.module';
import { serverError } from 'ish-core/store/core/error';
import { loadServerConfigSuccess } from 'ish-core/store/core/server-config';
import { CustomerStoreModule } from 'ish-core/store/customer/customer-store.module';
import { getPGID } from 'ish-core/store/customer/user';

import { ApiService, unpackEnvelope } from './api.service';
Expand Down Expand Up @@ -554,11 +553,7 @@ describe('Api Service', () => {
beforeEach(() => {
TestBed.configureTestingModule({
// https://angular.io/guide/http#testing-http-requests
imports: [
CoreStoreModule.forTesting(['configuration', 'serverConfig']),
CustomerStoreModule.forTesting('user'),
HttpClientTestingModule,
],
imports: [CoreStoreModule.forTesting(['configuration', 'serverConfig']), HttpClientTestingModule],
});

apiService = TestBed.inject(ApiService);
Expand Down
9 changes: 2 additions & 7 deletions src/app/core/store/customer/customer-store.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { Component } from '@angular/core';
import { TestBed } from '@angular/core/testing';
import { RouterTestingModule } from '@angular/router/testing';
import { TranslateModule } from '@ngx-translate/core';
Expand Down Expand Up @@ -106,9 +105,6 @@ describe('Customer Store', () => {
} as Promotion;

beforeEach(() => {
@Component({ template: 'dummy' })
class DummyComponent {}

const categoriesServiceMock = mock(CategoriesService);
when(categoriesServiceMock.getTopLevelCategories(anyNumber())).thenReturn(of(categoryTree()));

Expand Down Expand Up @@ -154,18 +150,17 @@ describe('Customer Store', () => {
when(productPriceServiceMock.getProductPrices(anything())).thenReturn(of([]));

TestBed.configureTestingModule({
declarations: [DummyComponent],
imports: [
CoreStoreModule.forTesting(['configuration', 'serverConfig'], true),
CustomerStoreModule,
RouterTestingModule.withRoutes([
{
path: 'account',
component: DummyComponent,
children: [],
},
{
path: 'checkout/address',
component: DummyComponent,
children: [],
},
]),
ShoppingStoreModule,
Expand Down
Loading

0 comments on commit a4e403c

Please sign in to comment.