added test classes for delete and rename examples

Signed-off-by: Stefan Petrovic <stefanp0618@gmail.com>
This commit is contained in:
Stefan Petrovic
2023-08-04 21:10:37 +02:00
parent 97b972978f
commit 1777c5f52b
3 changed files with 161 additions and 1 deletions
@@ -13,3 +13,67 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createFilesystemDeleteAction } from './delete';
import { getVoidLogger } from '@backstage/backend-common';
import { PassThrough } from 'stream';
import { resolve as resolvePath } from 'path';
import * as os from 'os';
import mockFs from 'mock-fs';
import fs from 'fs-extra';
import yaml from 'yaml';
import { examples } from './delete.examples';
const root = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir';
const workspacePath = resolvePath(root, 'my-workspace');
describe('fs:delete examples', () => {
const action = createFilesystemDeleteAction();
const files: string[] = yaml.parse(examples[0].example).steps[0].input.files;
const mockContext = {
input: {
files: files,
},
workspacePath,
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};
beforeEach(() => {
jest.restoreAllMocks();
mockFs({
[workspacePath]: {
[files[0]]: 'hello',
[files[1]]: 'world',
'a-folder': {
'unit-test-in-a-folder.js2': 'content',
},
},
});
});
afterEach(() => {
mockFs.restore();
});
it('should call fs.rm with the correct values', async () => {
files.forEach(file => {
const filePath = resolvePath(workspacePath, file);
const fileExists = fs.existsSync(filePath);
expect(fileExists).toBe(true);
});
await action.handler(mockContext);
files.forEach(file => {
const filePath = resolvePath(workspacePath, file);
const fileExists = fs.existsSync(filePath);
expect(fileExists).toBe(false);
});
});
});
@@ -13,3 +13,98 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import * as os from 'os';
import mockFs from 'mock-fs';
import { resolve as resolvePath } from 'path';
import { createFilesystemRenameAction } from './rename';
import { getVoidLogger } from '@backstage/backend-common';
import { PassThrough } from 'stream';
import fs from 'fs-extra';
import yaml from 'yaml';
import { examples } from './rename.examples';
const root = os.platform() === 'win32' ? 'C:\\rootDir' : '/rootDir';
const workspacePath = resolvePath(root, 'my-workspace');
describe('fs:rename examples', () => {
const action = createFilesystemRenameAction();
const files: { from: string; to: string }[] = yaml.parse(examples[0].example)
.steps[0].input.files;
const mockContext = {
input: {
files: files,
},
workspacePath,
logger: getVoidLogger(),
logStream: new PassThrough(),
output: jest.fn(),
createTemporaryDirectory: jest.fn(),
};
beforeEach(() => {
jest.restoreAllMocks();
mockFs({
[workspacePath]: {
[files[0].from]: 'hello',
[files[1].from]: 'world',
[files[2].from]: '!!!',
'file3Renamed.txt': 'I will be overwritten :(',
'a-folder': {
'file.md': 'content',
},
},
});
});
afterEach(() => {
mockFs.restore();
});
it('should call fs.move with the correct values', async () => {
mockContext.input.files.forEach(file => {
const filePath = resolvePath(workspacePath, file.from);
const fileExists = fs.existsSync(filePath);
expect(fileExists).toBe(true);
});
await action.handler(mockContext);
mockContext.input.files.forEach(file => {
const filePath = resolvePath(workspacePath, file.from);
const fileExists = fs.existsSync(filePath);
expect(fileExists).toBe(false);
});
});
it('should override when requested', async () => {
const sourceFile = files[2].from;
const destFile = files[2].to;
const sourceFilePath = resolvePath(workspacePath, sourceFile);
const destFilePath = resolvePath(workspacePath, destFile);
const sourceBeforeContent = fs.readFileSync(sourceFilePath, 'utf-8');
const destBeforeContent = fs.readFileSync(destFilePath, 'utf-8');
expect(sourceBeforeContent).not.toEqual(destBeforeContent);
await action.handler({
...mockContext,
input: {
files: [
{
from: sourceFile,
to: destFile,
overwrite: true,
},
],
},
});
const destAfterContent = fs.readFileSync(destFilePath, 'utf-8');
expect(sourceBeforeContent).toEqual(destAfterContent);
});
});
@@ -28,8 +28,9 @@ export const examples: TemplateExample[] = [
name: 'Rename files',
input: {
files: [
{ from: 'file.txt', to: 'fileRenamed.txt', overwrite: true },
{ from: 'file1.txt', to: 'file1Renamed.txt' },
{ from: 'file2.txt', to: 'file2Renamed.txt' },
{ from: 'file3.txt', to: 'file3Renamed.txt', overwrite: true },
],
},
},