smartarchive/test/test.ts

105 lines
2.9 KiB
TypeScript
Raw Normal View History

2023-07-26 14:13:33 +00:00
import { tap, expect } from '@push.rocks/tapbundle';
2020-03-16 14:38:17 +00:00
import * as path from 'path';
2023-07-26 14:13:33 +00:00
import * as smartpath from '@push.rocks/smartpath';
import * as smartfile from '@push.rocks/smartfile';
import * as smartrequest from '@push.rocks/smartrequest';
2020-03-13 20:22:56 +00:00
2020-03-16 14:38:17 +00:00
const testPlugins = {
path,
smartfile,
2021-04-19 11:49:25 +00:00
smartrequest,
2020-03-16 14:38:17 +00:00
};
const testPaths = {
2023-07-26 14:13:33 +00:00
nogitDir: testPlugins.path.join(
smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../.nogit/'
),
remoteDir: testPlugins.path.join(
smartpath.get.dirnameFromImportMetaUrl(import.meta.url),
'../.nogit/remote'
),
2020-03-16 15:13:58 +00:00
};
2020-03-16 14:38:17 +00:00
2022-04-04 12:15:51 +00:00
import * as smartarchive from '../ts/index.js';
2020-03-16 14:38:17 +00:00
tap.preTask('should prepare .nogit dir', async () => {
2020-03-16 17:04:17 +00:00
await testPlugins.smartfile.fs.ensureDir(testPaths.remoteDir);
2020-03-13 20:22:56 +00:00
});
2021-04-19 11:49:25 +00:00
tap.preTask('should prepare downloads', async (tools) => {
2020-03-16 14:38:17 +00:00
const downloadedFile: Buffer = (
2020-03-16 15:13:58 +00:00
await testPlugins.smartrequest.getBinary(
2023-07-26 14:13:33 +00:00
'https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz'
2020-03-16 15:13:58 +00:00
)
2020-03-16 14:38:17 +00:00
).body;
2020-03-16 15:13:58 +00:00
await testPlugins.smartfile.memory.toFs(
downloadedFile,
testPlugins.path.join(testPaths.nogitDir, 'test.tgz')
);
2020-03-16 14:38:17 +00:00
});
2020-03-17 00:21:05 +00:00
tap.test('should extract existing files on disk', async () => {
2020-03-16 14:38:17 +00:00
const testSmartarchive = new smartarchive.SmartArchive();
2021-04-19 11:49:25 +00:00
await testSmartarchive.extractArchiveFromFilePathToFs(
2020-03-16 14:38:17 +00:00
testPlugins.path.join(testPaths.nogitDir, 'test.tgz'),
testPlugins.path.join(testPaths.nogitDir)
);
});
2020-03-13 20:22:56 +00:00
2020-03-16 17:04:17 +00:00
tap.test('should download a package from the registry', async () => {
const testSmartarchive = new smartarchive.SmartArchive();
2021-04-19 11:49:25 +00:00
await testSmartarchive.extractArchiveFromUrlToFs(
2023-07-26 14:13:33 +00:00
'https://verdaccio.lossless.digital/@pushrocks%2fsmartfile/-/smartfile-7.0.11.tgz',
2021-04-19 11:49:25 +00:00
testPaths.remoteDir
);
});
2021-04-19 12:12:48 +00:00
tap.test('should extract a package using tarStream', async (tools) => {
const done = tools.defer();
2021-04-19 11:49:25 +00:00
const testSmartarchive = new smartarchive.SmartArchive();
const testTgzBuffer = (
await testPlugins.smartfile.Smartfile.fromFilePath(
testPlugins.path.join(testPaths.nogitDir, 'test.tgz')
)
).contentBuffer;
const extractionFileObservable = await testSmartarchive.extractArchiveFromBufferToObservable(
testTgzBuffer
);
2021-04-19 12:15:23 +00:00
const subscription = extractionFileObservable.subscribe(
(file) => {
console.log(file.path);
},
(err) => {
console.log(err);
},
() => {
done.resolve();
}
);
2021-04-19 12:12:48 +00:00
await done.promise;
});
tap.test('should extract a file from url to replaySubject', async (tools) => {
const done = tools.defer();
const testSmartarchive = new smartarchive.SmartArchive();
2021-04-19 12:15:23 +00:00
const extractionFileObservable = await testSmartarchive.extractArchiveFromUrlToObservable(
2023-07-26 14:13:33 +00:00
'https://verdaccio.lossless.digital/@pushrocks%2fwebsetup/-/websetup-2.0.14.tgz'
);
const subscription = extractionFileObservable.subscribe(
(file) => {
console.log(file.path);
},
(err) => {
console.log(err);
},
() => {
done.resolve();
}
2021-04-19 12:15:23 +00:00
);
2021-04-19 12:12:48 +00:00
await done.promise;
2020-03-16 17:04:17 +00:00
});
2020-03-13 20:22:56 +00:00
tap.start();