diff --git a/src/compilerServices/clangService.ts b/src/compilerServices/clangService.ts index 43eea7596..8e04be681 100644 --- a/src/compilerServices/clangService.ts +++ b/src/compilerServices/clangService.ts @@ -18,7 +18,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ -import { CompilerService, ServiceInput, ServiceOutput, Language } from "./types"; +import { CompilerService, ServiceInput, ServiceOutput, Language, InputFile } from "./types"; import { sendRequestJSON, ServiceTypes } from "./sendRequest"; import { decodeBinary } from "./utils"; @@ -31,23 +31,25 @@ export class ClangService implements CompilerService { async compile(input: ServiceInput): Promise { const files = Object.values(input.files); - if (files.length !== 1) { - throw new Error(`Supporting compilation of a single file, but ${files.length} file(s) found`); - } - const [ fileRef ] = Object.keys(input.files); - const src = files[0].content; - const from = this.lang; + const [fileRef] = Object.keys(input.files); + const inputFile = Object.keys(input.files); + const File = function(inputFile: string[], files: InputFile[]) { + const compileFile = []; + for (let i = 0; i < inputFile.length; i++) { + const f = { + type: inputFile[i].split(".").slice(-1)[0], + name: inputFile[i].split("/").slice(-1)[0], + options: input.options, + src: files[i].content, + }; + compileFile.push(f); + } + return compileFile; + }; const project = { output: "wasm", compress: true, - files: [ - { - type: from, - name: "file." + from, - options: input.options, - src - } - ] + files: File(inputFile, files) }; const result = await sendRequestJSON(project, ServiceTypes.Clang); const items: any = {}; diff --git a/tests/compilerServices/clangService.spec.ts b/tests/compilerServices/clangService.spec.ts index 887d86547..470101bf8 100644 --- a/tests/compilerServices/clangService.spec.ts +++ b/tests/compilerServices/clangService.spec.ts @@ -34,7 +34,7 @@ describe("Tests for clangService", () => { expect(sendRequestJSON).toHaveBeenCalledWith({ output: "wasm", compress: true, - files: [{ type: "c", name: "file.c", options: "options", src: "a" }] + files: [{ type: "c", name: "a.c", options: "options", src: "a" }] }, 2); expect(decodeBinary).toHaveBeenCalledWith("out"); expect(output).toEqual({ @@ -56,11 +56,31 @@ describe("Tests for clangService", () => { console: "error" }); }); - it("should throw an error when trying to compile more than one file", async () => { + it("should compile when trying to compile more than one file", async () => { const clangService = await createCompilerService(Language.C, Language.Wasm); - const input = { files: { "a.c": { content: "a" }, "b.c": { content: "b" }}}; - await expect(clangService.compile(input)) - .rejects - .toThrow("Supporting compilation of a single file, but 2 file(s) found"); + sendRequestJSON.mockImplementation(() => ({ + success: true, + output: "out", + tasks: [{ console }], + message: "response-message" + })); + const input = { files: { "a.c": { content: "a" }, "b.c": { content: "b" }}, options: "options" }; + const console = { log: jest.fn() }; + const output = await clangService.compile(input); + expect(sendRequestJSON).toHaveBeenCalledWith({ + output: "wasm", + compress: true, + files: [ + { type: "c", name: "a.c", options: "options", src: "a" }, + { type: "c", name: "b.c", options: "options", src: "b" } + ] + }, 2); + expect(decodeBinary).toHaveBeenCalledWith("out"); + expect(output).toEqual({ + success: true, + items: { "a.wasm": { content: "out", fileRef: "a.c", console, }}, + console: "response-message" + }); + decodeBinary.mockClear(); }); });