Merge pull request #7528 from backstage/rugvip/root-tests

cli: allow __dirname usage in backend tests and fix test runs from root
This commit is contained in:
Patrik Oldsberg
2021-10-12 10:51:56 +02:00
committed by GitHub
17 changed files with 73 additions and 105 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Update default backend ESLint configuration to allow usage of `__dirname` in tests.
@@ -135,13 +135,7 @@ describe('AwsS3UrlReader', () => {
'getObject',
Buffer.from(
require('fs').readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'awsS3',
'awsS3-mock-object.yaml',
),
path.resolve(__dirname, '__fixtures__/awsS3/awsS3-mock-object.yaml'),
),
),
);
@@ -187,13 +181,7 @@ describe('AwsS3UrlReader', () => {
'getObject',
Buffer.from(
require('fs').readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'awsS3',
'awsS3-mock-object.yaml',
),
path.resolve(__dirname, '__fixtures__/awsS3/awsS3-mock-object.yaml'),
),
),
);
@@ -249,13 +237,7 @@ describe('AwsS3UrlReader', () => {
'getObject',
Buffer.from(
require('fs').readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'awsS3',
'awsS3-mock-object.yaml',
),
path.resolve(__dirname, '__fixtures__/awsS3/awsS3-mock-object.yaml'),
),
),
);
@@ -152,7 +152,7 @@ describe('AzureUrlReader', () => {
describe('readTree', () => {
const repoBuffer = fs.readFileSync(
path.resolve('src', 'reading', '__fixtures__', 'mock-main.zip'),
path.resolve(__dirname, '__fixtures__/mock-main.zip'),
);
const processor = new AzureUrlReader(
@@ -264,7 +264,7 @@ describe('AzureUrlReader', () => {
describe('search', () => {
const repoBuffer = fs.readFileSync(
path.resolve('src', 'reading', '__fixtures__', 'mock-main.zip'),
path.resolve(__dirname, '__fixtures__/mock-main.zip'),
);
const processor = new AzureUrlReader(
@@ -104,20 +104,13 @@ describe('BitbucketUrlReader', () => {
describe('readTree', () => {
const repoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'bitbucket-repo-with-commit-hash.tar.gz',
__dirname,
'__fixtures__/bitbucket-repo-with-commit-hash.tar.gz',
),
);
const privateBitbucketRepoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'bitbucket-server-repo.tar.gz',
),
path.resolve(__dirname, '__fixtures__/bitbucket-server-repo.tar.gz'),
);
beforeEach(() => {
@@ -298,10 +291,8 @@ describe('BitbucketUrlReader', () => {
describe('search hosted', () => {
const repoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'bitbucket-repo-with-commit-hash.tar.gz',
__dirname,
'__fixtures__/bitbucket-repo-with-commit-hash.tar.gz',
),
);
@@ -386,12 +377,7 @@ describe('BitbucketUrlReader', () => {
describe('search private', () => {
const privateBitbucketRepoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'bitbucket-server-repo.tar.gz',
),
path.resolve(__dirname, '__fixtures__/bitbucket-server-repo.tar.gz'),
);
beforeEach(() => {
@@ -254,12 +254,7 @@ describe('GithubUrlReader', () => {
describe('readTree', () => {
const repoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'backstage-mock-etag123.tar.gz',
),
path.resolve(__dirname, '__fixtures__/backstage-mock-etag123.tar.gz'),
);
const reposGithubApiResponse = {
@@ -540,12 +535,7 @@ describe('GithubUrlReader', () => {
describe('search', () => {
const repoBuffer = fs.readFileSync(
path.resolve(
'src',
'reading',
'__fixtures__',
'backstage-mock-etag123.tar.gz',
),
path.resolve(__dirname, '__fixtures__/backstage-mock-etag123.tar.gz'),
);
const githubTreeContents: GhTreeResponse['tree'] = [
@@ -223,7 +223,7 @@ describe('GitlabUrlReader', () => {
describe('readTree', () => {
const archiveBuffer = fs.readFileSync(
path.resolve('src', 'reading', '__fixtures__', 'gitlab-archive.tar.gz'),
path.resolve(__dirname, '__fixtures__/gitlab-archive.tar.gz'),
);
const projectGitlabApiResponse = {
@@ -495,7 +495,7 @@ describe('GitlabUrlReader', () => {
describe('search', () => {
const archiveBuffer = fs.readFileSync(
path.resolve('src', 'reading', '__fixtures__', 'gitlab-archive.tar.gz'),
path.resolve(__dirname, '__fixtures__/gitlab-archive.tar.gz'),
);
const projectGitlabApiResponse = {
+2
View File
@@ -46,6 +46,8 @@ describe('paths', () => {
const dir = resolvePath(__dirname, '..');
const root = resolvePath(__dirname, '../../..');
jest.spyOn(process, 'cwd').mockReturnValue(dir);
const paths = findPaths(__dirname);
expect(paths.ownDir).toBe(dir);
+11 -6
View File
@@ -14,6 +14,15 @@
* limitations under the License.
*/
const globalRestrictedSyntax = [
{
message:
'Default import from winston is not allowed, import `* as winston` instead.',
selector:
'ImportDeclaration[source.value="winston"] ImportDefaultSpecifier',
},
];
module.exports = {
extends: [
'@spotify/eslint-config-base',
@@ -69,17 +78,12 @@ module.exports = {
// Avoid default import from winston as it breaks at runtime
'no-restricted-syntax': [
'error',
{
message:
'Default import from winston is not allowed, import `* as winston` instead.',
selector:
'ImportDeclaration[source.value="winston"] ImportDefaultSpecifier',
},
{
message:
"`__dirname` doesn't refer to the same dir in production builds, try `resolvePackagePath()` from `@backstage/backend-common` instead.",
selector: 'Identifier[name="__dirname"]',
},
...globalRestrictedSyntax,
],
},
overrides: [
@@ -103,6 +107,7 @@ module.exports = {
bundledDependencies: true,
},
],
'no-restricted-syntax': ['error', ...globalRestrictedSyntax],
},
},
],
+3 -2
View File
@@ -18,7 +18,7 @@ const fs = require('fs-extra');
const path = require('path');
const glob = require('util').promisify(require('glob'));
async function getProjectConfig(targetPath) {
async function getProjectConfig(targetPath, displayName) {
const configJsPath = path.resolve(targetPath, 'jest.config.js');
const configTsPath = path.resolve(targetPath, 'jest.config.ts');
// If the package has it's own jest config, we use that instead.
@@ -73,6 +73,7 @@ async function getProjectConfig(targetPath) {
const transformModulePattern = transformModules && `(?!${transformModules})`;
const options = {
displayName,
rootDir: path.resolve(targetPath, 'src'),
coverageDirectory: path.resolve(targetPath, 'coverage'),
collectCoverageFrom: ['**/*.{js,jsx,ts,tsx}', '!**/*.d.ts'],
@@ -143,7 +144,7 @@ async function getRootConfig() {
const packageData = await fs.readJson(packagePath);
const testScript = packageData.scripts && packageData.scripts.test;
if (testScript && testScript.includes('backstage-cli test')) {
return await getProjectConfig(projectPath);
return await getProjectConfig(projectPath, packageData.name);
}
return undefined;
@@ -31,15 +31,17 @@ describe('readAzureIntegrationConfig', () => {
data: Partial<AzureIntegrationConfig>,
): Promise<Config> {
const fullSchema = await loadConfigSchema({
dependencies: [require('../../package.json').name],
dependencies: ['@backstage/integration'],
});
const serializedSchema = fullSchema.serialize() as {
schemas: { path: string }[];
schemas: { value: { properties?: { integrations?: object } } }[];
};
const schema = await loadConfigSchema({
serialized: {
...serializedSchema, // grab the schema from this package only
schemas: serializedSchema.schemas.filter(s => s.path === 'config.d.ts'),
...serializedSchema, // only include schemas that apply to integrations
schemas: serializedSchema.schemas.filter(
s => s.value?.properties?.integrations,
),
},
});
const processed = schema.process(
@@ -31,15 +31,17 @@ describe('readBitbucketIntegrationConfig', () => {
data: Partial<BitbucketIntegrationConfig>,
): Promise<Config> {
const fullSchema = await loadConfigSchema({
dependencies: [require('../../package.json').name],
dependencies: ['@backstage/integration'],
});
const serializedSchema = fullSchema.serialize() as {
schemas: { path: string }[];
schemas: { value: { properties?: { integrations?: object } } }[];
};
const schema = await loadConfigSchema({
serialized: {
...serializedSchema, // grab the schema from this package only
schemas: serializedSchema.schemas.filter(s => s.path === 'config.d.ts'),
...serializedSchema, // only include schemas that apply to integrations
schemas: serializedSchema.schemas.filter(
s => s.value?.properties?.integrations,
),
},
});
const processed = schema.process(
@@ -31,15 +31,17 @@ describe('readGitHubIntegrationConfig', () => {
data: Partial<GitHubIntegrationConfig>,
): Promise<Config> {
const fullSchema = await loadConfigSchema({
dependencies: [require('../../package.json').name],
dependencies: ['@backstage/integration'],
});
const serializedSchema = fullSchema.serialize() as {
schemas: { path: string }[];
schemas: { value: { properties?: { integrations?: object } } }[];
};
const schema = await loadConfigSchema({
serialized: {
...serializedSchema, // grab the schema from this package only
schemas: serializedSchema.schemas.filter(s => s.path === 'config.d.ts'),
...serializedSchema, // only include schemas that apply to integrations
schemas: serializedSchema.schemas.filter(
s => s.value?.properties?.integrations,
),
},
});
const processed = schema.process(
@@ -31,15 +31,17 @@ describe('readGitLabIntegrationConfig', () => {
data: Partial<GitLabIntegrationConfig>,
): Promise<Config> {
const fullSchema = await loadConfigSchema({
dependencies: [require('../../package.json').name],
dependencies: ['@backstage/integration'],
});
const serializedSchema = fullSchema.serialize() as {
schemas: { path: string }[];
schemas: { value: { properties?: { integrations?: object } } }[];
};
const schema = await loadConfigSchema({
serialized: {
...serializedSchema, // grab the schema from this package only
schemas: serializedSchema.schemas.filter(s => s.path === 'config.d.ts'),
...serializedSchema, // only include schemas that apply to integrations
schemas: serializedSchema.schemas.filter(
s => s.value?.properties?.integrations,
),
},
});
const processed = schema.process(
@@ -37,13 +37,8 @@ AWSMock.mock(
Buffer.from(
require('fs').readFileSync(
path.resolve(
'src',
'ingestion',
'processors',
'__fixtures__',
'fileReaderProcessor',
'awsS3',
'awsS3-mock-object.txt',
__dirname,
'__fixtures__/fileReaderProcessor/awsS3/awsS3-mock-object.txt',
),
),
),
@@ -23,13 +23,7 @@ import {
import path from 'path';
describe('FileReaderProcessor', () => {
const fixturesRoot = path.join(
'src',
'ingestion',
'processors',
'__fixtures__',
'fileReaderProcessor',
);
const fixturesRoot = path.join(__dirname, '__fixtures__/fileReaderProcessor');
it('should load from file', async () => {
const processor = new FileReaderProcessor();
@@ -14,7 +14,6 @@
* limitations under the License.
*/
import { resolvePackagePath } from '@backstage/backend-common';
import { ConfigReader } from '@backstage/config';
import path from 'path';
import { ConfigLocationEntityProvider } from './ConfigLocationEntityProvider';
@@ -44,10 +43,7 @@ describe('ConfigLocationEntityProvider', () => {
{
entity: expect.objectContaining({
spec: {
target: path.join(
resolvePackagePath('@backstage/plugin-catalog-backend'),
'./lols.yaml',
),
target: path.join(process.cwd(), 'lols.yaml'),
type: 'file',
},
}),
@@ -15,10 +15,14 @@
*/
import os from 'os';
import { join as joinPath, resolve as resolvePath } from 'path';
import { join as joinPath } from 'path';
import fs from 'fs-extra';
import mockFs from 'mock-fs';
import { getVoidLogger, UrlReader } from '@backstage/backend-common';
import {
getVoidLogger,
resolvePackagePath,
UrlReader,
} from '@backstage/backend-common';
import { ScmIntegrations } from '@backstage/integration';
import { PassThrough } from 'stream';
import { fetchContents } from './helpers';
@@ -30,9 +34,9 @@ jest.mock('./helpers', () => ({
}));
const aBinaryFile = fs.readFileSync(
resolvePath(
'src',
'../fixtures/test-nested-template/public/react-logo192.png',
resolvePackagePath(
'@backstage/plugin-scaffolder-backend',
'fixtures/test-nested-template/public/react-logo192.png',
),
);