Skip to content
This repository has been archived by the owner on Dec 25, 2022. It is now read-only.

Commit

Permalink
fix: test files
Browse files Browse the repository at this point in the history
  • Loading branch information
imhele committed Jan 23, 2019
1 parent 3c12f99 commit 1a907be
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 47 deletions.
19 changes: 19 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"singleQuote": true,
"trailingComma": "all",
"printWidth": 120,
"overrides": [
{
"files": ".prettierrc",
"options": {
"parser": "json"
}
},
{
"files": ".stylelintrc",
"options": {
"parser": "json"
}
}
]
}
14 changes: 9 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,16 @@ export default function (api: IApi, options?: UmiPluginOssOptions) {
if (options.bijection || options.ignore.existsInOss) {
const existsFileArr = await syncFiles.list(prefix, api);
if (options.bijection) {
const delFileArr = existsFileArr.filter(filename => {
const delFileArr: string[] = existsFileArr.filter(filename => {
return !fileInfoArr.some(fileInfo => fileInfo[0] === filename);
});
api.log.success(`The following files will be delete:\n${delFileArr.join('\n')}`);
const deleteCosts = await syncFiles.delete(prefix, delFileArr, api);
api.log.success(`Deleted in ${deleteCosts / 1000}s`);
if (delFileArr.length) {
api.log.success(`The following files will be deleted:\n${delFileArr.join('\n')}`);
const deleteCosts = await syncFiles.delete(prefix, delFileArr, api);
api.log.success(`Deleted in ${deleteCosts / 1000}s`);
} else {
api.log.success(`There is nothing need to be deleted.`);
}
}
if (options.ignore.existsInOss) {
fileInfoArr = fileInfoArr.filter(fileInfo => {
Expand All @@ -147,7 +151,7 @@ export default function (api: IApi, options?: UmiPluginOssOptions) {
// Empty list
if (!fileInfoArr.length) {
// @TODO: bijection => delete files
return api.log.success('There is nothing need to upload.');
return api.log.success('There is nothing need to be uploaded.');
}

// upload and print results
Expand Down
84 changes: 46 additions & 38 deletions test/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'jest';
import { FileInfo, wait } from '../src/syncFiles';
import { IOnBuildSuccessFunc } from 'umi-plugin-types';
import { FileInfo } from '../src/syncFiles';
import UmiPluginOss, { handleAcl } from '../src/index';

jest.mock('fs');
Expand All @@ -18,7 +18,7 @@ export const umiApi = {
outputPath: '/dist/',
absOutputPath: '/home/dist/',
},
registerCommand: () => { },
registerCommand: () => {},
log: {
success: (...messages: string[]) => {
messageQueue.set(`${Date.now()}.${Math.random()}|success`, messages);
Expand All @@ -33,7 +33,9 @@ export const umiApi = {
debug: (message: string) => {
messageQueue.set(`${Date.now()}.${Math.random()}|debug`, [message]);
},
onBuildSuccess: (callback: IOnBuildSuccessFunc) => { callback(undefined); },
onBuildSuccess: (callback: IOnBuildSuccessFunc) => {
callback(undefined);
},
};

describe('test index', () => {
Expand All @@ -43,10 +45,7 @@ describe('test index', () => {
});

test('handleAcl', () => {
const fileInfoArr: FileInfo[] = [
['umi.js', '/home/umi.js', 'private'],
['test.js', '/home/test.js', 'private'],
];
const fileInfoArr: FileInfo[] = [['umi.js', '/home/umi.js', 'private'], ['test.js', '/home/test.js', 'private']];
expect(() => {
handleAcl(['test.js'], fileInfoArr, 'public-read');
}).not.toThrow();
Expand All @@ -61,20 +60,23 @@ describe('test index', () => {
expect(messageQueue.size).toBe(0);
});

test('UmiPluginOss with default options', () => {
test('UmiPluginOss with default options', async () => {
messageQueue.clear();
expect(() => {
UmiPluginOss(umiApi as any, {
accessKeyId: 'test',
accessKeySecret: 'test',
});
}).not.toThrow();
expect(messageQueue.size).toBe(1);
await wait(0.1);
expect(messageQueue.size).toBe(6);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('The following files will be uploaded to cdn.imhele.com/:\n'
+ 'umi.js private\n'
+ 'static/image.png private');
expect(messageQueue.get(keys[0])[0]).toBe(
'The following files will be uploaded to cdn.imhele.com/:\n' +
'umi.js private\n' +
'static/image.png private',
);
});

test('UmiPluginOss without cname and bucket', () => {
Expand All @@ -99,7 +101,7 @@ describe('test index', () => {
expect(keys[0].endsWith('error')).toBe(true);
});

test('UmiPluginOss with bucket', () => {
test('UmiPluginOss with bucket', async () => {
messageQueue.clear();
expect(() => {
UmiPluginOss(
Expand All @@ -120,15 +122,16 @@ describe('test index', () => {
},
);
}).not.toThrow();
expect(messageQueue.size).toBe(1);
await wait(0.1);
expect(messageQueue.size).toBe(6);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('The following files will be uploaded to imhele/:\n'
+ 'umi.js private\n'
+ 'static/image.png private');
expect(messageQueue.get(keys[0])[0]).toBe(
'The following files will be uploaded to imhele/:\n' + 'umi.js private\n' + 'static/image.png private',
);
});

test('UmiPluginOss with RegExp acl rule', () => {
test('UmiPluginOss with RegExp acl rule', async () => {
messageQueue.clear();
expect(() => {
UmiPluginOss(umiApi as any, {
Expand All @@ -139,12 +142,15 @@ describe('test index', () => {
},
});
}).not.toThrow();
expect(messageQueue.size).toBe(1);
await wait(0.1);
expect(messageQueue.size).toBe(6);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('The following files will be uploaded to cdn.imhele.com/:\n'
+ 'umi.js public-read\n'
+ 'static/image.png private');
expect(messageQueue.get(keys[0])[0]).toBe(
'The following files will be uploaded to cdn.imhele.com/:\n' +
'umi.js public-read\n' +
'static/image.png private',
);
});

test('UmiPluginOss with ignore filter', () => {
Expand All @@ -162,10 +168,10 @@ describe('test index', () => {
expect(messageQueue.size).toBe(1);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('There is nothing need to upload.');
expect(messageQueue.get(keys[0])[0]).toBe('There is nothing need to be uploaded.');
});

test('UmiPluginOss with bijection', () => {
test('UmiPluginOss with bijection', async () => {
messageQueue.clear();
expect(() => {
UmiPluginOss(umiApi as any, {
Expand All @@ -174,17 +180,20 @@ describe('test index', () => {
bijection: true,
});
}).not.toThrow();
setTimeout(() => {
expect(messageQueue.size).toBe(1);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('The following files will be uploaded to cdn.imhele.com/:\n'
+ 'umi.js private\n'
+ 'static/image.png private');
}, 10);
await wait(0.1);
expect(messageQueue.size).toBe(7);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
expect(keys[1].endsWith('success')).toBe(true);
expect(messageQueue.get(keys[0])[0]).toBe('There is nothing need to be deleted.');
expect(messageQueue.get(keys[1])[0]).toBe(
'The following files will be uploaded to cdn.imhele.com/:\n' +
'umi.js private\n' +
'static/image.png private',
);
});

test('UmiPluginOss with ignore existsInOss', () => {
test('UmiPluginOss with ignore existsInOss', async () => {
messageQueue.clear();
expect(() => {
UmiPluginOss(umiApi as any, {
Expand All @@ -195,10 +204,9 @@ describe('test index', () => {
},
});
}).not.toThrow();
setTimeout(() => {
expect(messageQueue.size).toBe(1);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
}, 10);
await wait(0.1);
expect(messageQueue.size).toBe(1);
const keys = Array.from(messageQueue.keys());
expect(keys[0].endsWith('success')).toBe(true);
});
});
9 changes: 5 additions & 4 deletions test/syncFiles.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,11 @@ describe('test syncFiles', () => {
};
const instance = new SyncFiles(options);
await instance.upload('', [], umiApi as any);
const time = await instance.upload('', [
['200', '/home/notexist/umi.js', 'private'],
['403', '/home/notexist/umi.js', 'private'],
], umiApi as any);
const time = await instance.upload(
'',
[['200', '/home/notexist/umi.js', 'private'], ['403', '/home/notexist/umi.js', 'private']],
umiApi as any,
);
expect(typeof time === 'number').toBe(true);
const keys = Array.from(messageQueue.keys());
const error = keys.find(k => k.endsWith('error'));
Expand Down

0 comments on commit 1a907be

Please sign in to comment.