-
Notifications
You must be signed in to change notification settings - Fork 574
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
test: migrate code tests to acceptance #5139
Changes from all commits
62067d4
761e948
92283a3
8bb10bd
1f55903
126335b
5ae71af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import * as express from 'express'; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. question: what's the reason for not extending edit: I think this comment may have something to do with it? If so, it might be worth adding something like a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A good question, |
||
import * as http from 'http'; | ||
import * as net from 'net'; | ||
|
||
export type FakeDeepCodeServer = { | ||
getRequests: () => express.Request[]; | ||
popRequest: () => express.Request; | ||
popRequests: (num: number) => express.Request[]; | ||
setCustomResponse: (next: Record<string, unknown>) => void; | ||
setFiltersResponse: (next: Record<string, unknown>) => void; | ||
setNextResponse: (r: any) => void; | ||
setNextStatusCode: (code: number) => void; | ||
setSarifResponse: (r: any) => void; | ||
listen: (callback: () => void) => void; | ||
restore: () => void; | ||
close: (callback: () => void) => void; | ||
getPort: () => number; | ||
}; | ||
|
||
export const fakeDeepCodeServer = (): FakeDeepCodeServer => { | ||
let filtersResponse: Record<string, unknown> | null = { | ||
configFiles: [], | ||
extensions: ['.java'], | ||
}; | ||
let sarifResponse: Record<string, unknown> | null = null; | ||
let requests: express.Request[] = []; | ||
// the status code to return for the next request, overriding statusCode | ||
let nextResponse: Record<string, unknown> | undefined = undefined; | ||
let nextStatusCode: number | undefined = undefined; | ||
let customResponse: Record<string, unknown> | undefined = undefined; | ||
let server: http.Server | undefined = undefined; | ||
const sockets = new Set(); | ||
|
||
const restore = () => { | ||
requests = []; | ||
customResponse = undefined; | ||
nextResponse = undefined; | ||
nextStatusCode = undefined; | ||
sarifResponse = null; | ||
filtersResponse = { configFiles: [], extensions: ['.java', '.js'] }; | ||
}; | ||
|
||
const getRequests = () => { | ||
return requests; | ||
}; | ||
|
||
const popRequest = () => { | ||
return requests.pop()!; | ||
}; | ||
|
||
const popRequests = (num: number) => { | ||
return requests.splice(requests.length - num, num); | ||
}; | ||
|
||
const setCustomResponse = (next: typeof customResponse) => { | ||
customResponse = next; | ||
}; | ||
|
||
const setFiltersResponse = (response: string | Record<string, unknown>) => { | ||
if (typeof response === 'string') { | ||
filtersResponse = JSON.parse(response); | ||
return; | ||
} | ||
filtersResponse = response; | ||
}; | ||
|
||
const setNextResponse = (response: string | Record<string, unknown>) => { | ||
if (typeof response === 'string') { | ||
nextResponse = JSON.parse(response); | ||
return; | ||
} | ||
nextResponse = response; | ||
}; | ||
|
||
const setNextStatusCode = (code: number) => { | ||
nextStatusCode = code; | ||
}; | ||
|
||
const setSarifResponse = (response: string | Record<string, unknown>) => { | ||
if (typeof response === 'string') { | ||
sarifResponse = JSON.parse(response); | ||
return; | ||
} | ||
sarifResponse = response; | ||
}; | ||
|
||
const app = express(); | ||
app.use((req, res, next) => { | ||
requests.push(req); | ||
next(); | ||
}); | ||
|
||
app.use((req, res, next) => { | ||
if (nextStatusCode) { | ||
const code = nextStatusCode; | ||
res.status(code); | ||
} | ||
|
||
if (nextResponse) { | ||
const response = nextResponse; | ||
res.send(response); | ||
return; | ||
} | ||
next(); | ||
}); | ||
|
||
app.get('/filters', (req, res) => { | ||
res.status(200); | ||
res.send(filtersResponse); | ||
}); | ||
|
||
app.post('/bundle', (req, res) => { | ||
res.status(200); | ||
|
||
res.send({ | ||
bundleHash: 'bundle-hash', | ||
missingFiles: [], | ||
}); | ||
}); | ||
|
||
app.post('/analysis', (req, res) => { | ||
res.status(200); | ||
res.send({ | ||
timing: { | ||
fetchingCode: 1, | ||
analysis: 1, | ||
queue: 1, | ||
}, | ||
coverage: [], | ||
status: 'COMPLETE', | ||
type: 'sarif', | ||
sarif: sarifResponse, | ||
}); | ||
}); | ||
|
||
const listenPromise = () => { | ||
return new Promise<void>((resolve) => { | ||
server = http.createServer(app).listen(resolve); | ||
|
||
server?.on('connection', (socket) => { | ||
sockets.add(socket); | ||
}); | ||
}); | ||
}; | ||
|
||
const listen = (callback: () => void) => { | ||
listenPromise().then(callback); | ||
}; | ||
|
||
const closePromise = () => { | ||
return new Promise<void>((resolve) => { | ||
if (!server) { | ||
resolve(); | ||
return; | ||
} | ||
server.close(() => resolve()); | ||
server = undefined; | ||
}); | ||
}; | ||
|
||
const close = (callback: () => void) => { | ||
for (const socket of sockets) { | ||
(socket as net.Socket)?.destroy(); | ||
sockets.delete(socket); | ||
} | ||
|
||
closePromise().then(callback); | ||
}; | ||
|
||
const getPort = () => { | ||
const address = server?.address(); | ||
if (address && typeof address === 'object') { | ||
return address.port; | ||
} | ||
throw new Error('port not found'); | ||
}; | ||
|
||
return { | ||
getRequests, | ||
popRequest, | ||
popRequests, | ||
setCustomResponse: setCustomResponse, | ||
setFiltersResponse, | ||
setSarifResponse, | ||
setNextResponse, | ||
setNextStatusCode, | ||
listen, | ||
restore, | ||
close, | ||
getPort, | ||
}; | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SnykCode", | ||
"semanticVersion": "1.0.0", | ||
"version": "1.0.0", | ||
"rules": [] | ||
} | ||
}, | ||
"results": [], | ||
"properties": { | ||
"coverage": [ | ||
{ | ||
"files": 8, | ||
"isSupported": true, | ||
"lang": "JavaScript" | ||
}, | ||
{ | ||
"files": 1, | ||
"isSupported": true, | ||
"lang": "HTML" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
console.log('shallow_empty'); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
{ | ||
"$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", | ||
"version": "2.1.0", | ||
"runs": [ | ||
{ | ||
"tool": { | ||
"driver": { | ||
"name": "SnykCode", | ||
"semanticVersion": "1.0.0", | ||
"version": "1.0.0", | ||
"rules": [] | ||
} | ||
}, | ||
"results": [], | ||
"properties": { | ||
"coverage": [ | ||
{ | ||
"files": 8, | ||
"isSupported": true, | ||
"lang": "JavaScript" | ||
}, | ||
{ | ||
"files": 1, | ||
"isSupported": true, | ||
"lang": "HTML" | ||
} | ||
] | ||
} | ||
} | ||
] | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Introduce the ability to configure the CODE_CLIENT_PROXY_URL via environment variables. This is aimed at making it easier to test.