Merge pull request #7649 from backstage/rugvip/noglobals
techdocs-common: avoid global type declarations
This commit is contained in:
@@ -18,8 +18,9 @@ import {
|
||||
ContainerGetPropertiesResponse,
|
||||
} from '@azure/storage-blob';
|
||||
import { EventEmitter } from 'events';
|
||||
import { IStorageFilesMock } from '../../src/testUtils/types';
|
||||
|
||||
const storage = global.storageFilesMock;
|
||||
const storage = global.storageFilesMock as IStorageFilesMock;
|
||||
|
||||
export class BlockBlobClient {
|
||||
private readonly blobName;
|
||||
|
||||
@@ -14,8 +14,9 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
import { Readable } from 'stream';
|
||||
import { IStorageFilesMock } from '../../src/testUtils/types';
|
||||
|
||||
const storage = global.storageFilesMock;
|
||||
const storage = global.storageFilesMock as IStorageFilesMock;
|
||||
|
||||
class GCSFile {
|
||||
private readonly path: string;
|
||||
|
||||
@@ -15,10 +15,11 @@
|
||||
*/
|
||||
import { EventEmitter } from 'events';
|
||||
import { ReadStream } from 'fs';
|
||||
import { IStorageFilesMock } from '../src/testUtils/types';
|
||||
|
||||
export { Credentials } from 'aws-sdk';
|
||||
|
||||
const storage = global.storageFilesMock;
|
||||
const storage = global.storageFilesMock as IStorageFilesMock;
|
||||
|
||||
export class S3 {
|
||||
constructor() {
|
||||
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare module NodeJS {
|
||||
interface Global {
|
||||
rootDir: string;
|
||||
storageFilesMock: IStorageFilesMock;
|
||||
}
|
||||
}
|
||||
-22
@@ -1,22 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
declare module NodeJS {
|
||||
interface Global {
|
||||
rootDir: string;
|
||||
storageFilesMock: IStorageFilesMock;
|
||||
}
|
||||
}
|
||||
@@ -14,47 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { StorageFilesMock } from './testUtils/StorageFilesMock';
|
||||
|
||||
const rootDir: string = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir';
|
||||
|
||||
const encoding = 'utf8';
|
||||
|
||||
class StorageFilesMock implements IStorageFilesMock {
|
||||
private files: Record<string, string>;
|
||||
|
||||
constructor() {
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
public emptyFiles(): void {
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
public fileExists(targetPath: string): boolean {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
const posixPath = filePath.split(path.posix.sep).join(path.sep);
|
||||
return this.files[posixPath] !== undefined;
|
||||
}
|
||||
|
||||
public readFile(targetPath: string): Buffer {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
return Buffer.from(this.files[filePath] ?? '', encoding);
|
||||
}
|
||||
|
||||
public writeFile(targetPath: string, sourcePath: string): void;
|
||||
public writeFile(targetPath: string, sourceBuffer: Buffer): void;
|
||||
public writeFile(targetPath: string, source: string | Buffer): void {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
if (typeof source === 'string') {
|
||||
this.files[filePath] = fs.readFileSync(source).toString(encoding);
|
||||
} else {
|
||||
this.files[filePath] = source.toString(encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
global.rootDir = rootDir;
|
||||
global.storageFilesMock = new StorageFilesMock();
|
||||
(global as any).rootDir = StorageFilesMock.rootDir;
|
||||
(global as any).storageFilesMock = new StorageFilesMock();
|
||||
|
||||
@@ -26,7 +26,7 @@ import { AwsS3Publish } from './awsS3';
|
||||
|
||||
// NOTE: /packages/techdocs-common/__mocks__ is being used to mock aws-sdk client library
|
||||
|
||||
const rootDir = global.rootDir;
|
||||
const rootDir = (global as any).rootDir; // Set by setupTests.ts
|
||||
|
||||
const getEntityRootDir = (entity: Entity) => {
|
||||
const {
|
||||
|
||||
@@ -26,7 +26,7 @@ import { AzureBlobStoragePublish } from './azureBlobStorage';
|
||||
|
||||
// NOTE: /packages/techdocs-common/__mocks__ is being used to mock Azure client library
|
||||
|
||||
const rootDir = global.rootDir;
|
||||
const rootDir = (global as any).rootDir; // Set by setupTests.ts
|
||||
|
||||
const getEntityRootDir = (entity: Entity) => {
|
||||
const {
|
||||
|
||||
@@ -26,7 +26,7 @@ import { GoogleGCSPublish } from './googleStorage';
|
||||
|
||||
// NOTE: /packages/techdocs-common/__mocks__ is being used to mock Google Cloud Storage client library
|
||||
|
||||
const rootDir = global.rootDir;
|
||||
const rootDir = (global as any).rootDir; // Set by setupTests.ts
|
||||
|
||||
const getEntityRootDir = (entity: Entity) => {
|
||||
const {
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import os from 'os';
|
||||
import path from 'path';
|
||||
import fs from 'fs-extra';
|
||||
import { IStorageFilesMock } from './types';
|
||||
|
||||
const rootDir: string = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir';
|
||||
|
||||
const encoding = 'utf8';
|
||||
|
||||
export class StorageFilesMock implements IStorageFilesMock {
|
||||
static rootDir = rootDir;
|
||||
|
||||
private files: Record<string, string>;
|
||||
|
||||
constructor() {
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
public emptyFiles(): void {
|
||||
this.files = {};
|
||||
}
|
||||
|
||||
public fileExists(targetPath: string): boolean {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
const posixPath = filePath.split(path.posix.sep).join(path.sep);
|
||||
return this.files[posixPath] !== undefined;
|
||||
}
|
||||
|
||||
public readFile(targetPath: string): Buffer {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
return Buffer.from(this.files[filePath] ?? '', encoding);
|
||||
}
|
||||
|
||||
public writeFile(targetPath: string, sourcePath: string): void;
|
||||
public writeFile(targetPath: string, sourceBuffer: Buffer): void;
|
||||
public writeFile(targetPath: string, source: string | Buffer): void {
|
||||
const filePath = path.join(rootDir, targetPath);
|
||||
if (typeof source === 'string') {
|
||||
this.files[filePath] = fs.readFileSync(source).toString(encoding);
|
||||
} else {
|
||||
this.files[filePath] = source.toString(encoding);
|
||||
}
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
interface IStorageFilesMock {
|
||||
export interface IStorageFilesMock {
|
||||
emptyFiles(): void;
|
||||
fileExists(targetPath: string): boolean;
|
||||
readFile(targetPath: string): Buffer;
|
||||
-24
@@ -1,24 +0,0 @@
|
||||
/*
|
||||
* Copyright 2020 The Backstage Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
interface IStorageFilesMock {
|
||||
emptyFiles(): void;
|
||||
fileExists(targetPath: string): boolean;
|
||||
readFile(targetPath: string): Buffer;
|
||||
writeFile(targetPath: string, sourcePath: string): void;
|
||||
writeFile(targetPath: string, sourceBuffer: Buffer): void;
|
||||
writeFile(targetPath: string, source: string | Buffer): void;
|
||||
}
|
||||
Reference in New Issue
Block a user