Migrate tests for formatting

This commit is contained in:
Mats Larsen
2024-11-30 12:07:18 +09:00
parent 3683b60513
commit 7252326213
3 changed files with 88 additions and 26 deletions
+5 -7
View File
@@ -43,18 +43,16 @@ export class FormattingController implements HttpController {
const formatter = this.formattingService.getFormatterById(req.params.tool);
// Ensure the target formatter exists
if (formatter === null) {
return res.status(422).send({exit: 2, answer: `No formatter for '${req.params.tool}'`});
return res.status(422).json({exit: 2, answer: `Unknown format tool '${req.params.tool}'`});
}
// Ensure there is source code to format
if (!req.body || !req.body.source) {
res.send({exit: 0, answer: ''});
res.status(400).json({exit: 0, answer: ''});
return;
}
// Ensure that the requested style is supported by the formatter
if (!formatter.isValidStyle(req.body.base)) {
return res
.status(422)
.send({exit: 3, answer: `Style '${req.body.base}' is not supported by '${req.params.tool}'`});
return res.status(422).json({exit: 3, answer: `Style '${req.body.base}' is not supported`});
}
// Do the formatting
try {
@@ -63,9 +61,9 @@ export class FormattingController implements HttpController {
tabWidth: req.body.tabWidth === undefined ? 4 : req.body.tabWidth,
baseStyle: req.body.base,
});
res.send({exit: formatted.code, answer: formatted.stdout || formatted.stderr || ''});
res.json({exit: formatted.code, answer: formatted.stdout || formatted.stderr || ''});
} catch (err: unknown) {
res.status(500).send({
res.status(500).json({
exit: 1,
thrown: true,
answer:
-19
View File
@@ -164,23 +164,4 @@ describe('API handling', () => {
.expect('Content-Type', /json/)
.expect(200, [languages['c++'], languages.pascal]);
});
it('should not go through with invalid tools', async () => {
await request(app)
.post('/api/format/invalid')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(422, {exit: 2, answer: "Unknown format tool 'invalid'"});
});
it('should not go through with invalid base styles', async () => {
await request(app)
.post('/api/format/formatt')
.send({
base: 'bad-base',
source: 'i am source',
})
.set('Accept', 'application/json')
.expect(422, {exit: 3, answer: "Style 'bad-base' is not supported"})
.expect('Content-Type', /json/);
});
});
@@ -0,0 +1,83 @@
// Copyright (c) 2024, Compiler Explorer Authors
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright notice,
// this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
import express from 'express';
import request from 'supertest';
import {beforeAll, describe, it} from 'vitest';
import {FormattingService} from '../../../lib/formatting-service.js';
import {FormattingController} from '../../../lib/handlers/api/formatting-controller.js';
import {fakeProps} from '../../../lib/properties.js';
describe('FormattingController', () => {
let app;
beforeAll(async () => {
app = express();
const formattingService = new FormattingService();
const formattingController = new FormattingController(formattingService);
await formattingService.initialize(
fakeProps({
formatters: 'formatt:badformatt',
'formatter.formatt.exe': process.platform === 'win32' ? 'cmd' : 'echo',
'formatter.formatt.type': 'clangformat',
'formatter.formatt.version': 'Release',
'formatter.formatt.name': 'FormatT',
}),
);
app.use(express.json());
app.use(formattingController.createRouter());
});
it('should not go through with invalid tools', async () => {
await request(app)
.post('/api/format/invalid')
.set('Accept', 'application/json')
.expect('Content-Type', /json/)
.expect(422, {exit: 2, answer: "Unknown format tool 'invalid'"});
});
it('should not go through with invalid base styles', async () => {
await request(app)
.post('/api/format/formatt')
.send({
base: 'bad-base',
source: 'i am source',
})
.set('Accept', 'application/json')
.expect(422, {exit: 3, answer: "Style 'bad-base' is not supported"})
.expect('Content-Type', /json/);
});
it('should reject requests with no source', async () => {
await request(app)
.post('/api/format/formatt')
.send({
base: 'bad-base',
})
.set('Accept', 'application/json')
.expect(400, {exit: 0, answer: ''})
.expect('Content-Type', /json/);
});
});