forked from team-discovery-channel/compose
-
Notifications
You must be signed in to change notification settings - Fork 1
/
endtoend.rest.test.ts
82 lines (72 loc) · 2.99 KB
/
endtoend.rest.test.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import {languages} from '../../src/api/languages'
import AdmZip from 'adm-zip'
import util from 'util'
import express from 'express';
import path from 'path';
import * as routes from '../../src/routes';
import fs from 'fs'
import http from 'http'
import * as child from 'child_process'
const request = require('supertest')
import rimraf = require('rimraf');
import upath from 'upath'
import { isFlowPredicate } from '@babel/types';
const baseDir = "./test/files";
describe('End to end REST testing per language', () => {
jest.setTimeout(20000)
let app:any, server:any;
const tmpDir = `${baseDir}/tmp`
beforeAll(done => {
if(!fs.existsSync(tmpDir))
{
fs.mkdirSync(tmpDir)
}
rimraf.sync(tmpDir+"/**/*")
app = express();
app.set('views', path.join(__dirname, 'views'));
app.set('view engine', 'ejs');
routes.register(app);
server = http.createServer(app);
server.listen(done);
});
afterAll(done => {
rimraf(tmpDir+"/**/*",()=>{server.close(done);})
});
test("REST calls on each language over test files", async (done)=>{
const promises:Array<Promise<boolean>> = []
Object.keys(languages).forEach((lang)=>{
const ext:string = languages[lang].getExtensions()[0]
const langTestFiles = `${baseDir}/${lang}`
const testfiles:string[] = fs.readdirSync(langTestFiles).filter(fn=>fn!=="config.json");
const config = JSON.parse(fs.readFileSync(langTestFiles+"/config.json").toString())
if(typeof config.command !== "string"){
config.command = config.command[(process.platform === "win32")?"win32":"else"];
}
testfiles.forEach(async(dir)=>{
const zip:AdmZip = new AdmZip()
zip.addLocalFolder(`${langTestFiles}/${dir}`)
fs.writeFileSync(`${tmpDir}/${lang}_${dir}.zip`,zip.toBuffer())
promises.push(new Promise(async (resolve,reject)=>{
const script:fs.WriteStream = fs.createWriteStream(`${tmpDir}/${dir}${ext}`)
script.on("finish",()=>{
const result:string = child.execSync(util.format(config.command,`${tmpDir}/${dir}${ext}`),{cwd:".", timeout:2000}).toString()
if(result === config[dir]){
resolve(true)
}
else{
resolve(false)
}
})
request(app).post('/api/v1/compose')
.field("language", lang)
.field("entry",config.entry)
.attach("file",`${tmpDir}/${lang}_${dir}.zip`)
.pipe(script)
}))
})
})
const results:boolean[] = await Promise.all(promises)
expect(results).toEqual(expect.arrayContaining([true]))
done()
})
});