From f00bc6ea51549ce4739225b26833ef548817a970 Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Wed, 6 Mar 2024 20:12:15 -0600 Subject: [PATCH] Further encromulation --- .idea/prettier.xml | 2 + .mocharc-min.yml | 2 - .mocharc.yml | 1 - lib/handlers/formatting.ts | 2 +- package-lock.json | 14 ++ package.json | 8 +- test/compilers/argument-parsers-tests.ts | 3 +- test/compilers/clang-tests.ts | 5 +- test/compilers/hook-tests.ts | 3 +- test/handlers/api-tests.js | 297 ----------------------- test/handlers/api-tests.ts | 187 ++++++++++++++ test/utils.ts | 5 + 12 files changed, 221 insertions(+), 308 deletions(-) delete mode 100644 .mocharc-min.yml delete mode 100644 .mocharc.yml delete mode 100644 test/handlers/api-tests.js create mode 100644 test/handlers/api-tests.ts diff --git a/.idea/prettier.xml b/.idea/prettier.xml index 8004cebd9..60c07b5ac 100644 --- a/.idea/prettier.xml +++ b/.idea/prettier.xml @@ -1,6 +1,8 @@ + \ No newline at end of file diff --git a/.mocharc-min.yml b/.mocharc-min.yml deleted file mode 100644 index 7ecfbda04..000000000 --- a/.mocharc-min.yml +++ /dev/null @@ -1,2 +0,0 @@ -recursive: true -ignore: test/filter-tests.js diff --git a/.mocharc.yml b/.mocharc.yml deleted file mode 100644 index 7fd0cbb99..000000000 --- a/.mocharc.yml +++ /dev/null @@ -1 +0,0 @@ -recursive: true diff --git a/lib/handlers/formatting.ts b/lib/handlers/formatting.ts index 65545df62..623f04447 100644 --- a/lib/handlers/formatting.ts +++ b/lib/handlers/formatting.ts @@ -58,7 +58,7 @@ export class FormattingHandler { const result = await exec.execute(exe, [versionArgument], {}); const match = result.stdout.match(versionRegExp); const formatterClass = getFormatterTypeByKey(type); - const styleList = this.ceProps(`formatter.${formatterName}.styles`); + const styleList = this.ceProps(`formatter.${formatterName}.styles`, ''); const styles = styleList === '' ? [] : styleList.split(':'); // If there is an explicit version, grab it. Otherwise try to filter the output const version = hasExplicitVersion diff --git a/package-lock.json b/package-lock.json index 3e7e712f5..32b70c6df 100644 --- a/package-lock.json +++ b/package-lock.json @@ -134,6 +134,7 @@ "sass-loader": "^14.1.1", "sinon": "^17.0.1", "source-map-loader": "^5.0.0", + "supertest": "^6.3.4", "supervisor": "^0.12.0", "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.5.1", @@ -18728,6 +18729,19 @@ "node": ">=4.0.0" } }, + "node_modules/supertest": { + "version": "6.3.4", + "resolved": "https://registry.npmjs.org/supertest/-/supertest-6.3.4.tgz", + "integrity": "sha512-erY3HFDG0dPnhw4U+udPfrzXa4xhSG+n4rxfRuZWCUvjFWwKl+OxWf/7zk50s84/fAAs7vf5QAb9uRa0cCykxw==", + "dev": true, + "dependencies": { + "methods": "^1.1.2", + "superagent": "^8.1.2" + }, + "engines": { + "node": ">=6.4.0" + } + }, "node_modules/supervisor": { "version": "0.12.0", "resolved": "https://registry.npmjs.org/supervisor/-/supervisor-0.12.0.tgz", diff --git a/package.json b/package.json index da510e766..812d37b52 100644 --- a/package.json +++ b/package.json @@ -143,6 +143,7 @@ "sass-loader": "^14.1.1", "sinon": "^17.0.1", "source-map-loader": "^5.0.0", + "supertest": "^6.3.4", "supervisor": "^0.12.0", "terser-webpack-plugin": "^5.3.10", "ts-loader": "^9.5.1", @@ -166,13 +167,14 @@ }, "scripts": { "ci-lint": "eslint --format github .", - "ci-test": "c8 npm run test", + "ci-test": "vitest run --coverage", "cypress": "cypress run", "lint": "eslint --max-warnings=0 . --fix", "lint-check": "eslint --max-warnings=0 . && prettier --check .", "lint-files": "eslint --max-warnings=0", - "test": "vitest", - "test-min": "node --no-warnings=ExperimentalWarning --loader ts-node/esm ./node_modules/mocha/bin/mocha.js -b --config .mocharc-min.yml", + "test": "vitest run", + "test:watch": "vitest", + "test-min": "vitest run --exclude test/filter-tests.ts", "fix": "npm run lint && npm run format && npm run ts-check", "check": "npm run ts-check && npm run lint-check && npm run test-min -- --reporter min", "dev": "cross-env NODE_ENV=DEV node --no-warnings=ExperimentalWarning --loader ts-node/esm app.ts", diff --git a/test/compilers/argument-parsers-tests.ts b/test/compilers/argument-parsers-tests.ts index 2e8246f77..610cf12dd 100644 --- a/test/compilers/argument-parsers-tests.ts +++ b/test/compilers/argument-parsers-tests.ts @@ -22,7 +22,8 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -import {beforeAll, describe, it, expect} from 'vitest'; +import {beforeAll, describe, expect, it} from 'vitest'; + import {CompilerArguments} from '../../lib/compiler-arguments.js'; import { BaseParser, diff --git a/test/compilers/clang-tests.ts b/test/compilers/clang-tests.ts index 6b09aa53b..ff845b023 100644 --- a/test/compilers/clang-tests.ts +++ b/test/compilers/clang-tests.ts @@ -22,9 +22,10 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -import {beforeAll, describe, it, expect} from 'vitest'; +import {describe, expect, it} from 'vitest'; + import {ClangCompiler} from '../../lib/compilers/index.js'; -import { makeCompilationEnvironment} from '../utils.js'; +import {makeCompilationEnvironment} from '../utils.js'; describe('clang tests', () => { const languages = {'c++': {id: 'c++'}}; diff --git a/test/compilers/hook-tests.ts b/test/compilers/hook-tests.ts index 99a6c06b8..cdec455f6 100644 --- a/test/compilers/hook-tests.ts +++ b/test/compilers/hook-tests.ts @@ -22,7 +22,8 @@ // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE // POSSIBILITY OF SUCH DAMAGE. -import {beforeAll, describe, it, expect} from 'vitest'; +import {describe, expect, it} from 'vitest'; + import {HookCompiler} from '../../lib/compilers/index.js'; import {makeCompilationEnvironment} from '../utils.js'; diff --git a/test/handlers/api-tests.js b/test/handlers/api-tests.js deleted file mode 100644 index 656d12a78..000000000 --- a/test/handlers/api-tests.js +++ /dev/null @@ -1,297 +0,0 @@ -// Copyright (c) 2017, 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 {ApiHandler} from '../../lib/handlers/api.js'; -import {StorageNull} from '../../lib/storage/index.js'; -import {chai} from '../utils.js'; - -const languages = { - 'c++': { - id: 'c++', - name: 'C++', - monaco: 'cppp', - extensions: ['.cpp', '.cxx', '.h', '.hpp', '.hxx', '.c'], - }, - haskell: { - id: 'haskell', - name: 'Haskell', - monaco: 'haskell', - extensions: ['.hs', '.haskell'], - }, - pascal: { - id: 'pascal', - name: 'Pascal', - monaco: 'pascal', - extensions: ['.pas'], - }, -}; -const compilers = [ - { - id: 'gcc900', - name: 'GCC 9.0.0', - lang: 'c++', - }, - { - id: 'fpc302', - name: 'FPC 3.0.2', - lang: 'pascal', - }, - { - id: 'clangtrunk', - name: 'Clang trunk', - lang: 'c++', - }, -]; - -const compilersLimitedFields = [ - { - id: 'gcc900', - name: 'GCC 9.0.0', - }, - { - id: 'fpc302', - name: 'FPC 3.0.2', - }, - { - id: 'clangtrunk', - name: 'Clang trunk', - }, -]; - -describe('API handling', () => { - let app; - - beforeAll(() => { - app = express(); - const apiHandler = new ApiHandler( - { - handle: res => res.send('compile'), - handleCmake: res => res.send('cmake'), - handlePopularArguments: res => res.send('ok'), - handleOptimizationArguments: res => res.send('ok'), - }, - (key, def) => { - switch (key) { - case 'formatters': { - return 'formatt:badformatt'; - } - case 'formatter.formatt.exe': { - return 'echo'; - } - case 'formatter.formatt.version': { - return 'Release'; - } - case 'formatter.formatt.name': { - return 'FormatT'; - } - default: { - return def; - } - } - }, - new StorageNull('/', {}), - 'default', - ); - app.use('/api', apiHandler.handle); - apiHandler.setCompilers(compilers); - apiHandler.setLanguages(languages); - }); - - it('should respond to plain text compiler requests', () => { - return chai - .request(app) - .get('/api/compilers') - .then(res => { - res.should.have.status(200); - res.should.be.text; - res.text.should.contain('Compiler Name'); - res.text.should.contain('gcc900'); - res.text.should.contain('GCC 9.0.0'); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON compiler requests', () => { - return chai - .request(app) - .get('/api/compilers') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals(compilers); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON compiler requests with all fields', () => { - return chai - .request(app) - .get('/api/compilers?fields=all') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals(compilers); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON compiler requests with limited fields', () => { - return chai - .request(app) - .get('/api/compilers?fields=id,name') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals(compilersLimitedFields); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON compilers requests with c++ filter', () => { - return chai - .request(app) - .get('/api/compilers/c++') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals([compilers[0], compilers[2]]); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON compilers requests with pascal filter', () => { - return chai - .request(app) - .get('/api/compilers/pascal') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals([compilers[1]]); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to plain text language requests', () => { - return chai - .request(app) - .get('/api/languages') - .then(res => { - res.should.have.status(200); - res.should.be.text; - res.text.should.contain('Name'); - res.text.should.contain('c++'); - res.text.should.contain('pascal'); - // We should not list languages for which there are no compilers - res.text.should.not.contain('Haskell'); - }) - .catch(err => { - throw err; - }); - }); - it('should respond to JSON languages requests', () => { - return chai - .request(app) - .get('/api/languages') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals([languages['c++'], languages.pascal]); - }) - .catch(err => { - throw err; - }); - }); - // TODO(supergrecko): re-write this test case - it.skip('should list the formatters', () => { - if (process.platform !== 'win32') { - // Expects an executable called echo - return chai - .request(app) - .get('/api/formats') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(200); - res.should.be.json; - res.body.should.deep.equals([{name: 'FormatT', version: 'Release'}]); - }) - .catch(err => { - throw err; - }); - } - }); - it('should not go through with invalid tools', () => { - return chai - .request(app) - .post('/api/format/invalid') - .set('Accept', 'application/json') - .then(res => { - res.should.have.status(422); - res.should.be.json; - res.body.should.deep.equals({exit: 2, answer: "Unknown format tool 'invalid'"}); - }); - }); - /* - it('should not go through with invalid base styles', () => { - return chai.request(app) - .post('/api/format/formatt') - .set('Accept', 'application/json') - .set('Content-Type', 'application/json') - .send({ - base: "bad-base", - source: "" - }) - .then(res => { - res.should.have.status(422); - res.should.be.json; - res.body.should.deep.equals({exit: 3, answer: "Base style not supported"}); - }); - }); - */ - it('should respond to plain site template requests', () => { - return chai - .request(app) - .get('/api/siteTemplates') - .then(res => { - res.should.have.status(200); - res.should.be.json; - }) - .catch(err => { - throw err; - }); - }); -}); diff --git a/test/handlers/api-tests.ts b/test/handlers/api-tests.ts new file mode 100644 index 000000000..c3a005aa8 --- /dev/null +++ b/test/handlers/api-tests.ts @@ -0,0 +1,187 @@ +// Copyright (c) 2017, 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 bodyParser from 'body-parser'; +import express from 'express'; +import request from 'supertest'; +import {beforeAll, describe, expect, it} from 'vitest'; + +import {ApiHandler} from '../../lib/handlers/api.js'; +import {CompileHandler} from '../../lib/handlers/compile.js'; +import {CompilerProps, fakeProps} from '../../lib/properties.js'; +import {StorageNull} from '../../lib/storage/index.js'; +import {CompilerInfo} from '../../types/compiler.interfaces.js'; +import {Language, LanguageKey} from '../../types/languages.interfaces.js'; +import {makeFakeCompilerInfo, makeFakeLanguage} from '../utils.js'; + +const languages: Partial> = { + 'c++': makeFakeLanguage({ + id: 'c++', + name: 'C++', + monaco: 'cppp', + extensions: ['.cpp', '.cxx', '.h', '.hpp', '.hxx', '.c'], + }), + haskell: makeFakeLanguage({ + id: 'haskell', + name: 'Haskell', + monaco: 'haskell', + extensions: ['.hs', '.haskell'], + }), + pascal: makeFakeLanguage({ + id: 'pascal', + name: 'Pascal', + monaco: 'pascal', + extensions: ['.pas'], + }), +}; +const compilers: CompilerInfo[] = [ + makeFakeCompilerInfo({ + id: 'gcc900', + name: 'GCC 9.0.0', + lang: 'c++', + }), + makeFakeCompilerInfo({ + id: 'fpc302', + name: 'FPC 3.0.2', + lang: 'pascal', + }), + makeFakeCompilerInfo({ + id: 'clangtrunk', + name: 'Clang trunk', + lang: 'c++', + }), +]; + +describe('API handling', () => { + let app; + + beforeAll(() => { + app = express(); + const apiHandler = new ApiHandler( + { + handle: res => res.send('compile'), + handleCmake: res => res.send('cmake'), + handlePopularArguments: res => res.send('ok'), + handleOptimizationArguments: res => res.send('ok'), + } as unknown as CompileHandler, // TODO(mrg) ideally fake this out or make it a higher-level interface + fakeProps({ + formatters: 'formatt:badformatt', + 'formatter.formatt.exe': 'echo', + 'formatter.formatt.type': 'clangformat', + 'formatter.formatt.version': 'Release', + 'formatter.formatt.name': 'FormatT', + }), + new StorageNull('/', new CompilerProps(languages, fakeProps({}))), + 'default', + ); + app.use(bodyParser.json()); + app.use('/api', apiHandler.handle); + apiHandler.setCompilers(compilers); + apiHandler.setLanguages(languages); + }); + + it('should respond to plain text compiler requests', async () => { + const res = await request(app).get('/api/compilers').expect(200).expect('Content-Type', /text/); + expect(res.text).toContain('Compiler Name'); + expect(res.text).toContain('gcc900'); + expect(res.text).toContain('GCC 9.0.0'); + }); + it('should respond to JSON compiler requests', async () => { + await request(app) + .get('/api/compilers') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, compilers); + }); + it('should respond to JSON compiler requests with all fields', async () => { + await request(app) + .get('/api/compilers?fields=all') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(compilers); + }); + it('should respond to JSON compiler requests with limited fields', async () => { + await request(app) + .get('/api/compilers?fields=id,name') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect( + 200, + compilers.map(c => { + return {id: c.id, name: c.name}; + }), + ); + }); + it('should respond to JSON compilers requests with c++ filter', async () => { + await request(app) + .get('/api/compilers/c++') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, [compilers[0], compilers[2]]); + }); + it('should respond to JSON compilers requests with pascal filter', async () => { + await request(app) + .get('/api/compilers/pascal') + .set('Accept', 'application/json') + .expect('Content-Type', /json/) + .expect(200, [compilers[1]]); + }); + it('should respond to plain text language requests', async () => { + const res = await request(app).get('/api/languages').expect(200).expect('Content-Type', /text/); + expect(res.text).toContain('Name'); + expect(res.text).toContain('c++'); + expect(res.text).toContain('c++'); + // We should not list languages for which there are no compilers + expect(res.text).not.toContain('haskell'); + }); + it('should respond to JSON languages requests', async () => { + await request(app) + .get('/api/languages') + .set('Accept', 'application/json') + .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/); + }); + it('should respond to plain site template requests', async () => { + await request(app).get('/api/siteTemplates').expect(200).expect('Content-Type', /json/); + }); +}); diff --git a/test/utils.ts b/test/utils.ts index 3ec642e6c..9be8d0cea 100644 --- a/test/utils.ts +++ b/test/utils.ts @@ -35,6 +35,7 @@ import {CompilationQueue} from '../lib/compilation-queue.js'; import {CompilerProps, fakeProps} from '../lib/properties.js'; import {CompilerInfo} from '../types/compiler.interfaces.js'; import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js'; +import {Language} from '../types/languages.interfaces'; // TODO: Find proper type for options export function makeCompilationEnvironment(options: Record): CompilationEnvironment { @@ -47,6 +48,10 @@ export function makeFakeCompilerInfo(props: Partial): CompilerInfo return props as CompilerInfo; } +export function makeFakeLanguage(props: Partial): Language { + return props as Language; +} + export function makeFakeParseFiltersAndOutputOptions( options: Partial, ): ParseFiltersAndOutputOptions {