From 0daae02461e74fab6a962effd4e96d193cfbb658 Mon Sep 17 00:00:00 2001 From: Matt Godbolt Date: Thu, 3 Apr 2025 14:33:18 +0100 Subject: [PATCH] Consolidate BaseCompiler test files into a single comprehensive file This commit consolidates all BaseCompiler test files into a single comprehensive file: - base-compiler-tests.ts (original) - base-compiler-tests-new.ts (new compilation tests) - base-compiler-exec-tests.ts (execution tests) - base-compiler-utils-tests.ts (utility tests) The tests are organized into logical sections with clear headers, making it easier to understand and maintain the BaseCompiler tests. This addresses cross-platform compatibility by ensuring Windows-specific path handling is properly taken into account in relevant tests. --- test/base-compiler-exec-tests.ts | 706 ------------- test/base-compiler-tests-new.ts | 459 --------- test/base-compiler-tests.ts | 1521 ++++++++++++++++++++++------- test/base-compiler-utils-tests.ts | 220 ----- 4 files changed, 1182 insertions(+), 1724 deletions(-) delete mode 100644 test/base-compiler-exec-tests.ts delete mode 100644 test/base-compiler-tests-new.ts delete mode 100644 test/base-compiler-utils-tests.ts diff --git a/test/base-compiler-exec-tests.ts b/test/base-compiler-exec-tests.ts deleted file mode 100644 index b5871ac9a..000000000 --- a/test/base-compiler-exec-tests.ts +++ /dev/null @@ -1,706 +0,0 @@ -// Copyright (c) 2025, 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 fs from 'node:fs/promises'; -import path from 'node:path'; - -import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; - -import {BaseCompiler} from '../lib/base-compiler.js'; -import {CompilationEnvironment} from '../lib/compilation-env.js'; -import * as exec from '../lib/exec.js'; -import * as utils from '../lib/utils.js'; -import type {FiledataPair} from '../types/compilation/compilation.interfaces.js'; -import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js'; - -import {createMockExecutor} from './mock/mock-utils.js'; -import { - makeCompilationEnvironment, - makeFakeCompilerInfo, - makeFakeParseFiltersAndOutputOptions, - newTempDir, -} from './utils.js'; - -const languages = { - 'c++': {id: 'c++'}, - rust: {id: 'rust'}, -} as const; - -describe('BaseCompiler execution', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let mockExec: ReturnType; - let execSpy: any; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - // Mock the findBadOptions function - ce.findBadOptions = vi.fn().mockReturnValue([]); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - supportsExecute: true, - supportsBinary: true, - }); - - compiler = new BaseCompiler(compilationInfo, ce); - // Create a temporary directory for testing - await newTempDir(); - - // Set up our mocks - mockExec = createMockExecutor( - new Map([ - [ - '/fake/compiler/path -Wall -O2 -g -o output.s -S input.cpp', - { - code: 0, - stdout: 'Compilation successful', - stderr: '', - }, - ], - [ - '/fake/compiler/path -Wall -O2 -g input.cpp -o a.out', - { - code: 0, - stdout: 'Binary compilation successful', - stderr: '', - }, - ], - ]), - ); - - vi.spyOn(exec, 'execute').mockImplementation(mockExec); - execSpy = vi.spyOn(compiler, 'exec'); - - // Mock the exec method with a fully typed result for compilation - execSpy.mockImplementation(async () => { - return { - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }; - }); - - // Mock filesystem operations - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); - - // In BaseCompiler, binary execution is mocked indirectly through exec - // We don't call sandbox directly but through the compiler's internal methods - vi.spyOn(exec, 'sandbox').mockResolvedValue({ - code: 0, - stdout: 'Program executed successfully', - stderr: '', - timedOut: false, - truncated: false, - filenameTransform: (x: string) => x, - execTime: 0, - okToCache: true, - }); - - // Need to mock handleExecution within the compiler - vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { - return { - code: 0, - didExecute: true, - stdout: [{text: 'Program executed successfully'}], - stderr: [], - buildResult: { - code: 0, - stdout: [{text: 'Binary compilation successful'}], - stderr: [], - }, - }; - }); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should successfully compile and execute code', async () => { - const source = 'int main() { return 42; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution - }); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, // bypassCache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(result.code).toBe(0); - expect(result.execResult).toBeDefined(); - expect(result.execResult?.didExecute).toBe(true); - expect(result.execResult?.code).toBe(0); - expect(result.execResult?.stdout).toEqual([{text: 'Program executed successfully'}]); - }); - - it('should handle execution failures with non-zero return code', async () => { - const source = 'int main() { return 1; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution - }); - - // Override handleExecution mock to simulate execution failure - vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { - return { - code: 1, - didExecute: true, - stdout: [], - stderr: [{text: 'Execution failed with return code 1'}], - buildResult: { - code: 0, - stdout: [{text: 'Binary compilation successful'}], - stderr: [], - }, - }; - }); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); // Compilation succeeded - expect(result.execResult).toBeDefined(); - expect(result.execResult?.didExecute).toBe(true); - expect(result.execResult?.code).toBe(1); // Execution failed - expect(result.execResult?.stderr).toEqual([{text: 'Execution failed with return code 1'}]); - }); - - it('should handle compilation failures for execution', async () => { - const source = 'int main() { error_function(); return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution - }); - - // Mock handleExecution to simulate binary compilation failure - vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { - return { - code: -1, - didExecute: false, - stdout: [], - stderr: [{text: 'Build failed'}], - buildResult: { - code: 1, - stdout: [], - stderr: [{text: 'error: use of undeclared identifier "error_function"'}], - }, - }; - }); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); // Assembly compilation succeeded - expect(result.execResult).toBeDefined(); - expect(result.execResult?.didExecute).toBe(false); - expect(result.execResult?.buildResult?.code).toBe(1); - expect(result.execResult?.buildResult?.stderr).toEqual([ - {text: 'error: use of undeclared identifier "error_function"'}, - ]); - }); - - it('should handle timeouts during execution', async () => { - const source = 'int main() { while(1); return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution - }); - - // Mock handleExecution to simulate execution timeout - vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { - return { - code: -1, - didExecute: true, - stdout: [], - stderr: [{text: 'Execution time out'}], - timedOut: true, - buildResult: { - code: 0, - stdout: [{text: 'Binary compilation successful'}], - stderr: [], - }, - }; - }); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); // Compilation succeeded - expect(result.execResult).toBeDefined(); - expect(result.execResult?.didExecute).toBe(true); - expect(result.execResult?.timedOut).toBe(true); - expect(result.execResult?.stderr).toEqual([{text: 'Execution time out'}]); - }); - - it('should pass execution parameters to the compiler', async () => { - const source = 'int main(int argc, char** argv) { return argc > 1 ? 0 : 1; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution - }); - - // Create a spy to capture execution params - const handleExecutionSpy = vi.spyOn(compiler, 'handleExecution' as any); - - // Mock return value for execution - handleExecutionSpy.mockResolvedValue({ - code: 0, - didExecute: true, - stdout: [{text: 'Program executed successfully'}], - stderr: [], - buildResult: { - code: 0, - stdout: [{text: 'Binary compilation successful'}], - stderr: [], - }, - }); - - await compiler.compile(source, options, {}, filters, false as any, [], {}, [], [] as FiledataPair[]); - - // Verify execution was called with the parameters - expect(handleExecutionSpy).toHaveBeenCalled(); - // In the actual implementation, handleExecution would receive the exec params - // but since we're mocking it, we just verify it was called - }); - - it('should not execute when compiler does not support execution', async () => { - // Create a new compiler that doesn't support execution - const noExecCompilerInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - supportsExecute: false, // No execution support - supportsBinary: true, - }); - - const noExecCompiler = new BaseCompiler(noExecCompilerInfo, ce); - vi.spyOn(noExecCompiler, 'exec').mockImplementation(async () => { - return { - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }; - }); - - // Mock handleExecution to indicate compiler doesn't support execution - vi.spyOn(noExecCompiler, 'handleExecution' as any).mockImplementation(async () => { - return { - code: -1, - didExecute: false, - stdout: [], - stderr: [{text: 'Compiler does not support execution'}], - buildResult: { - code: 0, - stdout: [], - stderr: [], - }, - }; - }); - - const source = 'int main() { return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - execute: true, // Request execution despite no support - }); - - const result = await noExecCompiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); - expect(result.execResult).toBeDefined(); - expect(result.execResult?.didExecute).toBe(false); - expect(result.execResult?.stderr).toEqual([{text: 'Compiler does not support execution'}]); - }); -}); - -describe('BaseCompiler error handling', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let execSpy: any; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - ce.findBadOptions = vi.fn().mockReturnValue([]); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - }); - - compiler = new BaseCompiler(compilationInfo, ce); - execSpy = vi.spyOn(compiler, 'exec'); - - // Mock filesystem operations - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should handle compilation timeouts properly', async () => { - // Simulate a timeout during compilation - execSpy.mockResolvedValue({ - code: -1, - stdout: '', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: true, - truncated: false, - }); - - const source = 'int main() { return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(-1); - expect(result.timedOut).toBe(true); - // The actual message might be different, but we know it should indicate compilation failure - expect(result.asm[0].text).toContain(''); - }); - - it('should handle output truncation', async () => { - // Simulate truncated output - execSpy.mockResolvedValue({ - code: 0, - stdout: 'Truncated stdout', - stderr: 'Truncated stderr', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: true, - }); - - const source = 'int main() { return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); - expect(result.truncated).toBe(true); - // The actual assembly text may vary in different environments - expect(result.stdout).toEqual([{text: 'Truncated stdout'}]); - expect(result.stderr).toEqual([{text: 'Truncated stderr'}]); - }); - - it('should handle corrupted output files', async () => { - // Successful compilation - execSpy.mockResolvedValue({ - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - // But readFile throws an error - vi.spyOn(fs, 'readFile').mockRejectedValue(new Error('Could not read output file')); - - const source = 'int main() { return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(0); // Compilation succeeded - // The actual error message may contain the specific error details - expect(result.asm[0].text).toContain('<'); - }); - - it('should handle source validation', async () => { - // Mock the checkSource method for testing - vi.spyOn(compiler, 'checkSource').mockImplementation((source: string) => { - if (source.includes('')) { - return ':1:1: no absolute or relative includes please'; - } - return null; - }); - - // Create a simple test case for source validation - const validSource = '#include \nint main() { return 0; }'; - const invalidSource = '#include '; - - // For valid source, checkSource should return null - expect(compiler.checkSource(validSource)).toBeNull(); - - // For invalid source with absolute path in include, it should return an error message - const result = compiler.checkSource(invalidSource); - expect(result).not.toBeNull(); - expect(result).toContain('no absolute or relative includes'); - }); - - it('should handle compiler failures with memory safety errors', async () => { - execSpy.mockResolvedValue({ - code: 139, // SIGSEGV exit code - stdout: '', - stderr: 'Segmentation fault', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - const source = 'int main() { return 0; }'; - const options = ['']; - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - false as any, - [], - {}, - [], - [] as FiledataPair[], - ); - - expect(result.code).toBe(139); - expect(result.asm).toEqual([{text: '', source: null, labels: []}]); - expect(result.stderr).toEqual([{text: 'Segmentation fault'}]); - }); -}); - -describe('BaseCompiler include handling', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let outputFileSpy: any; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - ce.findBadOptions = vi.fn().mockReturnValue([]); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - }); - - compiler = new BaseCompiler(compilationInfo, ce); - // Create a temporary directory for testing - await newTempDir(); - - // Mock filesystem operations - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); - - outputFileSpy = vi.spyOn(utils, 'outputTextFile').mockResolvedValue(); - - // Mock the exec method with a fully typed result - vi.spyOn(compiler, 'exec').mockImplementation(async () => { - return { - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }; - }); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should properly prepare input with additional files', async () => { - // Access the protected method directly to test it - const writeAllFiles = async (dirPath: string, source: string, files: any[]) => { - return await (compiler as any).writeAllFiles(dirPath, source, files, {}); - }; - - const source = '#include "header.h"\nint main() { return TEST_VALUE; }'; - const additionalFiles = [ - { - filename: 'header.h', - contents: '#define TEST_VALUE 42', - }, - ]; - - // Mock the utility functions - outputFileSpy.mockClear(); - - // Call the method directly - await writeAllFiles('/tmp/test', source, additionalFiles); - - // Verify the main source file was written - expect(fs.writeFile).toHaveBeenCalled(); - - // Verify the output file utility was called for the header - expect(outputFileSpy).toHaveBeenCalledWith(expect.stringContaining('header.h'), '#define TEST_VALUE 42'); - }); - - it('should correctly handle paths in additional files', async () => { - // Test getExtraFilepath directly for path validation - const getExtraFilepath = (dirPath: string, filename: string) => { - return (compiler as any).getExtraFilepath(dirPath, filename); - }; - - // Valid paths - expect(getExtraFilepath('/tmp/test', 'header.h')).toBe(path.join('/tmp/test', 'header.h')); - expect(getExtraFilepath('/tmp/test', 'subdir/header.h')).toBe(path.join('/tmp/test', 'subdir', 'header.h')); - - // Invalid paths with directory traversal - expect(() => getExtraFilepath('/tmp/test', '../header.h')).toThrow(); - expect(() => getExtraFilepath('/tmp/test', 'subdir/../../header.h')).toThrow(); - }); -}); diff --git a/test/base-compiler-tests-new.ts b/test/base-compiler-tests-new.ts deleted file mode 100644 index 1b9b663b0..000000000 --- a/test/base-compiler-tests-new.ts +++ /dev/null @@ -1,459 +0,0 @@ -// Copyright (c) 2025, 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 fs from 'node:fs/promises'; - -import {type Mock, afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; - -import {BaseCompiler} from '../lib/base-compiler.js'; -import {CompilationEnvironment} from '../lib/compilation-env.js'; -import * as exec from '../lib/exec.js'; -import type {CompilationResult} from '../types/compilation/compilation.interfaces.js'; -import type {FiledataPair} from '../types/compilation/compilation.interfaces.js'; -import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js'; - -import {createMockExecutor} from './mock/mock-utils.js'; -import {makeCompilationEnvironment, makeFakeCompilerInfo, makeFakeParseFiltersAndOutputOptions} from './utils.js'; - -// Import the BypassCache enum -import {BypassCache} from '../types/compilation/compilation.interfaces.js'; - -const languages = { - 'c++': {id: 'c++'}, - rust: {id: 'rust'}, -} as const; - -describe('BaseCompiler core functionality', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let mockExec: ReturnType; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - // Mock the findBadOptions function - ce.findBadOptions = vi.fn().mockReturnValue([]); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - supportsExecute: true, - supportsBinary: true, - }); - - compiler = new BaseCompiler(compilationInfo, ce); - - // Set up our mocks - mockExec = createMockExecutor( - new Map([ - [ - '/fake/compiler/path -Wall -O2 -g -o output.s -S input.cpp', - { - code: 0, - stdout: 'Compilation successful', - stderr: '', - }, - ], - [ - '/fake/compiler/path -Wall -O2 -g input.cpp -o a.out', - { - code: 0, - stdout: 'Binary compilation successful', - stderr: '', - }, - ], - ]), - ); - - vi.spyOn(exec, 'execute').mockImplementation(mockExec); - - // Mock the exec method with a fully typed result - vi.spyOn(compiler, 'exec').mockImplementation(async () => { - return { - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }; - }); - - // Mock filesystem operations - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should compile code successfully', async () => { - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // bypassCache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(result.code).toBe(0); - expect(result.asm.length).toBeGreaterThan(0); - }); - - it('should handle compilation failures', async () => { - // Override the exec mock to simulate a compilation failure - vi.spyOn(compiler, 'exec').mockResolvedValueOnce({ - code: 1, - stdout: '', - stderr: "error: unknown type name 'foo'", - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - const source = 'foo bar() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // bypassCache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(result.code).toBe(1); - expect(result.asm).toEqual([{text: '', source: null, labels: []}]); - }); -}); - -describe('BaseCompiler caching', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let cacheGetSpy: Mock; - let cachePutSpy: Mock; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - doCache: true, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - // Completely mock the cache methods directly rather than through spies - ce.cacheGet = vi.fn().mockResolvedValue(null); - ce.cachePut = vi.fn().mockResolvedValue(null); - ce.findBadOptions = vi.fn().mockReturnValue([]); - - cacheGetSpy = ce.cacheGet as Mock; - cachePutSpy = ce.cachePut as Mock; - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - }); - - compiler = new BaseCompiler(compilationInfo, ce); - - // Mock compiler execution - vi.spyOn(compiler, 'exec').mockResolvedValue({ - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should try to fetch from cache when caching is enabled', async () => { - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // NOT bypassing cache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(cacheGetSpy).toHaveBeenCalled(); - }); - - it('should store results in cache when compilation succeeds with okToCache', async () => { - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // NOT bypassing cache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(cachePutSpy).toHaveBeenCalled(); - }); - - it('should not store results in cache when okToCache is false', async () => { - vi.spyOn(compiler, 'exec').mockResolvedValue({ - code: 0, - stdout: 'Compilation successful', - stderr: '', - okToCache: false, // Setting okToCache to false - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // NOT bypassing cache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(cachePutSpy).not.toHaveBeenCalled(); - }); - - it('should return cached results when found in cache', async () => { - const cachedResult: CompilationResult = { - code: 0, - stdout: [{text: 'Cached stdout'}], - stderr: [{text: 'Cached stderr'}], - asm: [{text: 'Cached assembly', source: null, labels: []}], - timedOut: false, - compilationOptions: ['cached', 'options'], - inputFilename: 'cached.cpp', - // outputFilename is removed as it's not in CompilationResult type - executableFilename: 'cached.out', - // packagedFiles removed as it's not in CompilationResult type - optOutput: undefined, - tools: [], - popularArguments: {}, - okToCache: true, - }; - - cacheGetSpy.mockResolvedValueOnce(cachedResult); - - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); - - const result = await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, // NOT bypassing cache - [], // backendOptions - {}, // filters - [], // tools - [] as FiledataPair[], // executionParams - ); - - expect(result).toBe(cachedResult); - // exec should not be called as we got a cache hit - expect(compiler.exec).not.toHaveBeenCalled(); - }); -}); - -describe('BaseCompiler demangle', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let execSpy: any; // Use any type to avoid Mock type issues - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - languages, - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - // Mock the findBadOptions function - ce.findBadOptions = vi.fn().mockReturnValue([]); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - demangler: '/fake/demangler', - demanglerType: 'cpp', - demanglerArgs: ['-n'], // Add demanglerArgs property - }); - - compiler = new BaseCompiler(compilationInfo, ce); - - // Set up our mocks - execSpy = vi.spyOn(compiler, 'exec'); - - // Mock compiler assembly compilation - execSpy.mockResolvedValueOnce({ - code: 0, - stdout: 'Assembly compilation successful', - stderr: '', - okToCache: true, - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); - vi.spyOn(fs, 'readFile').mockResolvedValue('_Z3foov:\n ret\n'); - - // Mock check source method to prevent validation issues - vi.spyOn(compiler, 'checkSource').mockReturnValue(null); - - // Mock the postProcessAsm to simulate demangling - vi.spyOn(compiler, 'postProcessAsm').mockImplementation(async result => { - if (result) { - // Just create a simple demangled output - result.asm = [{text: 'foo():\n ret\n', source: null, labels: []}]; - } - return result; - }); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - it('should demangle symbols when requested', async () => { - // Mock demangler execution - execSpy.mockResolvedValueOnce({ - code: 0, - stdout: 'foo():\n', - stderr: '', - filenameTransform: (x: string) => x, - execTime: 0, - timedOut: false, - truncated: false, - }); - - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - demangle: true, // Request demangling - }); - - const result = await compiler.compile( - source, - options, - {}, - filters, - BypassCache.None, - [], - {}, - [], - [] as FiledataPair[], - ); - - // Result should have demangled output from our mock - expect(result.asm[0].text).toContain('foo():'); - }); - - it('should not demangle when not requested', async () => { - const source = 'int main() { return 42; }'; - const options = ['']; // Array of strings - const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ - demangle: false, // Do not request demangling - }); - - await compiler.compile(source, options, {}, filters, BypassCache.None, [], {}, [], [] as FiledataPair[]); - - // Check only one call to exec was made (for compilation, not demangling) - expect(execSpy).toHaveBeenCalledTimes(1); - }); -}); diff --git a/test/base-compiler-tests.ts b/test/base-compiler-tests.ts index 198c39ddb..b02d7e47e 100644 --- a/test/base-compiler-tests.ts +++ b/test/base-compiler-tests.ts @@ -1,4 +1,4 @@ -// Copyright (c) 2018, Compiler Explorer Authors +// Copyright (c) 2018 and 2025, Compiler Explorer Authors // All rights reserved. // // Redistribution and use in source and binary forms, with or without @@ -21,11 +21,17 @@ // 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. +// +// IMPORTANT NOTE: This file consolidates tests from: +// - base-compiler-tests.ts (original) +// - base-compiler-tests-new.ts (new compilation tests) +// - base-compiler-exec-tests.ts (execution tests) +// - base-compiler-utils-tests.ts (utility tests) import fs from 'node:fs/promises'; import path from 'node:path'; -import {afterAll, beforeAll, describe, expect, it} from 'vitest'; +import {type Mock, afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, vi} from 'vitest'; import {BaseCompiler} from '../lib/base-compiler.js'; import {BuildEnvSetupBase} from '../lib/buildenvsetup/index.js'; @@ -33,16 +39,22 @@ import {CompilationEnvironment} from '../lib/compilation-env.js'; import {ClangCompiler} from '../lib/compilers/clang.js'; import {RustCompiler} from '../lib/compilers/rust.js'; import {Win32Compiler} from '../lib/compilers/win32.js'; +import * as exec from '../lib/exec.js'; import * as props from '../lib/properties.js'; +import * as utils from '../lib/utils.js'; import {splitArguments} from '../shared/common-utils.js'; -import {CompilationResult} from '../types/compilation/compilation.interfaces.js'; +import {BypassCache, CompilationResult} from '../types/compilation/compilation.interfaces.js'; +import type {FiledataPair} from '../types/compilation/compilation.interfaces.js'; import {CompilerOverrideType, ConfiguredOverrides} from '../types/compilation/compiler-overrides.interfaces.js'; import {CompilerInfo} from '../types/compiler.interfaces.js'; +import {ParseFiltersAndOutputOptions} from '../types/features/filters.interfaces.js'; +import {createMockExecutor} from './mock/mock-utils.js'; import { makeCompilationEnvironment, makeFakeCompilerInfo, makeFakeParseFiltersAndOutputOptions, + newTempDir, shouldExist, } from './utils.js'; @@ -51,6 +63,9 @@ const languages = { rust: {id: 'rust'}, } as const; +// ====================================================================== +// Basic tests for BaseCompiler fundamental functionality +// ====================================================================== describe('Basic compiler invariants', () => { let ce: CompilationEnvironment; let compiler: BaseCompiler; @@ -76,6 +91,7 @@ describe('Basic compiler invariants', () => { expect(compiler.optOutputRequested(['please', 'recognize', '-fsave-optimization-record'])).toBe(true); expect(compiler.optOutputRequested(['please', "don't", 'recognize'])).toBe(false); }); + it('should allow comments next to includes (Bug #874)', () => { expect(compiler.checkSource('#include // std::(sin, cos, ...)')).toBeNull(); const badSource = compiler.checkSource('#include //Muehehehe'); @@ -83,6 +99,7 @@ describe('Basic compiler invariants', () => { expect(badSource).toEqual(':1:1: no absolute or relative includes please'); } }); + it('should not warn of path-likes outside C++ includes (Bug #3045)', () => { function testIncludeG(text: string) { expect(compiler.checkSource(text)).toBeNull(); @@ -94,6 +111,7 @@ describe('Basic compiler invariants', () => { testIncludeG('#include // for std::ranges::range<...> and std::ranges::range_type_v<...>'); testIncludeG('#include // /home/'); }); + it('should not allow path C++ includes', () => { function testIncludeNotG(text: string) { expect(compiler.checkSource(text)).toEqual(':1:1: no absolute or relative includes please'); @@ -104,6 +122,7 @@ describe('Basic compiler invariants', () => { testIncludeNotG('#include <../fish.config> // for std::is_same_v<...>'); testIncludeNotG('#include <./> // for std::ranges::range<...> and std::ranges::range_type_v<...>'); }); + it('should skip version check if forced to', async () => { const newConfig: Partial = {...info, explicitVersion: '123'}; const forcedVersionCompiler = new BaseCompiler(newConfig as CompilerInfo, ce); @@ -112,10 +131,1159 @@ describe('Basic compiler invariants', () => { }); }); -describe('Compiler execution', () => { +// ====================================================================== +// BaseCompiler core compilation functionality +// ====================================================================== +describe('BaseCompiler core functionality', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let mockExec: ReturnType; + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + languages, + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + // Mock the findBadOptions function + ce.findBadOptions = vi.fn().mockReturnValue([]); + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + supportsExecute: true, + supportsBinary: true, + }); + + compiler = new BaseCompiler(compilationInfo, ce); + + // Set up our mocks + mockExec = createMockExecutor( + new Map([ + [ + '/fake/compiler/path -Wall -O2 -g -o output.s -S input.cpp', + { + code: 0, + stdout: 'Compilation successful', + stderr: '', + }, + ], + [ + '/fake/compiler/path -Wall -O2 -g input.cpp -o a.out', + { + code: 0, + stdout: 'Binary compilation successful', + stderr: '', + }, + ], + ]), + ); + + vi.spyOn(exec, 'execute').mockImplementation(mockExec); + + // Mock the exec method with a fully typed result + vi.spyOn(compiler, 'exec').mockImplementation(async () => { + return { + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }; + }); + + // Mock filesystem operations + vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); + vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); + + // Mock check source method to prevent validation issues + vi.spyOn(compiler, 'checkSource').mockReturnValue(null); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + it('should compile code successfully', async () => { + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // bypassCache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(result.code).toBe(0); + expect(result.asm.length).toBeGreaterThan(0); + }); + + it('should handle compilation failures', async () => { + // Override the exec mock to simulate a compilation failure + vi.spyOn(compiler, 'exec').mockResolvedValueOnce({ + code: 1, + stdout: '', + stderr: "error: unknown type name 'foo'", + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + const source = 'foo bar() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // bypassCache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(result.code).toBe(1); + expect(result.asm).toEqual([{text: '', source: null, labels: []}]); + }); +}); + +// ====================================================================== +// BaseCompiler caching behavior +// ====================================================================== +describe('BaseCompiler caching', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let cacheGetSpy: Mock; + let cachePutSpy: Mock; + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + languages, + doCache: true, + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + // Completely mock the cache methods directly rather than through spies + ce.cacheGet = vi.fn().mockResolvedValue(null); + ce.cachePut = vi.fn().mockResolvedValue(null); + ce.findBadOptions = vi.fn().mockReturnValue([]); + + cacheGetSpy = ce.cacheGet as Mock; + cachePutSpy = ce.cachePut as Mock; + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + }); + + compiler = new BaseCompiler(compilationInfo, ce); + + // Mock compiler execution + vi.spyOn(compiler, 'exec').mockResolvedValue({ + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); + vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); + + // Mock check source method to prevent validation issues + vi.spyOn(compiler, 'checkSource').mockReturnValue(null); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + it('should try to fetch from cache when caching is enabled', async () => { + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // NOT bypassing cache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(cacheGetSpy).toHaveBeenCalled(); + }); + + it('should store results in cache when compilation succeeds with okToCache', async () => { + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // NOT bypassing cache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(cachePutSpy).toHaveBeenCalled(); + }); + + it('should not store results in cache when okToCache is false', async () => { + vi.spyOn(compiler, 'exec').mockResolvedValue({ + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: false, // Setting okToCache to false + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // NOT bypassing cache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(cachePutSpy).not.toHaveBeenCalled(); + }); + + it('should return cached results when found in cache', async () => { + const cachedResult: CompilationResult = { + code: 0, + stdout: [{text: 'Cached stdout'}], + stderr: [{text: 'Cached stderr'}], + asm: [{text: 'Cached assembly', source: null, labels: []}], + timedOut: false, + compilationOptions: ['cached', 'options'], + inputFilename: 'cached.cpp', + executableFilename: 'cached.out', + optOutput: undefined, + tools: [], + popularArguments: {}, + okToCache: true, + }; + + cacheGetSpy.mockResolvedValueOnce(cachedResult); + + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, // NOT bypassing cache + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(result).toBe(cachedResult); + // exec should not be called as we got a cache hit + expect(compiler.exec).not.toHaveBeenCalled(); + }); +}); + +// ====================================================================== +// Demangling functionality +// ====================================================================== +describe('BaseCompiler demangle', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let execSpy: any; // Use any type to avoid Mock type issues + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + languages, + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + // Mock the findBadOptions function + ce.findBadOptions = vi.fn().mockReturnValue([]); + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + demangler: '/fake/demangler', + demanglerType: 'cpp', + demanglerArgs: ['-n'], // Add demanglerArgs property + }); + + compiler = new BaseCompiler(compilationInfo, ce); + + // Set up our mocks + execSpy = vi.spyOn(compiler, 'exec'); + + // Mock compiler assembly compilation + execSpy.mockResolvedValueOnce({ + code: 0, + stdout: 'Assembly compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); + vi.spyOn(fs, 'readFile').mockResolvedValue('_Z3foov:\n ret\n'); + + // Mock check source method to prevent validation issues + vi.spyOn(compiler, 'checkSource').mockReturnValue(null); + + // Mock the postProcessAsm to simulate demangling + vi.spyOn(compiler, 'postProcessAsm').mockImplementation(async result => { + if (result) { + // Just create a simple demangled output + result.asm = [{text: 'foo():\n ret\n', source: null, labels: []}]; + } + return result; + }); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + it('should demangle symbols when requested', async () => { + // Mock demangler execution + execSpy.mockResolvedValueOnce({ + code: 0, + stdout: 'foo():\n', + stderr: '', + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + demangle: true, // Request demangling + }); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + // Result should have demangled output from our mock + expect(result.asm[0].text).toContain('foo():'); + }); + + it('should not demangle when not requested', async () => { + const source = 'int main() { return 42; }'; + const options = ['']; // Array of strings + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + demangle: false, // Do not request demangling + }); + + await compiler.compile(source, options, {}, filters, BypassCache.None, [], {}, [], [] as FiledataPair[]); + + // Check only one call to exec was made (for compilation, not demangling) + expect(execSpy).toHaveBeenCalledTimes(1); + }); +}); + +// ====================================================================== +// Execution functionality +// ====================================================================== +describe('BaseCompiler execution', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let mockExec: ReturnType; + let execSpy: any; + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + languages, + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + // Mock the findBadOptions function + ce.findBadOptions = vi.fn().mockReturnValue([]); + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + supportsExecute: true, + supportsBinary: true, + }); + + compiler = new BaseCompiler(compilationInfo, ce); + // Create a temporary directory for testing + await newTempDir(); + + // Set up our mocks + mockExec = createMockExecutor( + new Map([ + [ + '/fake/compiler/path -Wall -O2 -g -o output.s -S input.cpp', + { + code: 0, + stdout: 'Compilation successful', + stderr: '', + }, + ], + [ + '/fake/compiler/path -Wall -O2 -g input.cpp -o a.out', + { + code: 0, + stdout: 'Binary compilation successful', + stderr: '', + }, + ], + ]), + ); + + vi.spyOn(exec, 'execute').mockImplementation(mockExec); + execSpy = vi.spyOn(compiler, 'exec'); + + // Mock the exec method with a fully typed result for compilation + execSpy.mockImplementation(async () => { + return { + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }; + }); + + // Mock filesystem operations + vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); + vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); + + // In BaseCompiler, binary execution is mocked indirectly through exec + // We don't call sandbox directly but through the compiler's internal methods + vi.spyOn(exec, 'sandbox').mockResolvedValue({ + code: 0, + stdout: 'Program executed successfully', + stderr: '', + timedOut: false, + truncated: false, + filenameTransform: (x: string) => x, + execTime: 0, + okToCache: true, + }); + + // Need to mock handleExecution within the compiler + vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { + return { + code: 0, + didExecute: true, + stdout: [{text: 'Program executed successfully'}], + stderr: [], + buildResult: { + code: 0, + stdout: [{text: 'Binary compilation successful'}], + stderr: [], + }, + }; + }); + + // Mock check source method to prevent validation issues + vi.spyOn(compiler, 'checkSource').mockReturnValue(null); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + it('should successfully compile and execute code', async () => { + const source = 'int main() { return 42; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + execute: true, // Request execution + }); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], // backendOptions + {}, // filters + [], // tools + [] as FiledataPair[], // executionParams + ); + + expect(result.code).toBe(0); + expect(result.execResult).toBeDefined(); + expect(result.execResult?.didExecute).toBe(true); + expect(result.execResult?.code).toBe(0); + expect(result.execResult?.stdout).toEqual([{text: 'Program executed successfully'}]); + }); + + it('should handle execution failures with non-zero return code', async () => { + const source = 'int main() { return 1; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + execute: true, // Request execution + }); + + // Override handleExecution mock to simulate execution failure + vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { + return { + code: 1, + didExecute: true, + stdout: [], + stderr: [{text: 'Execution failed with return code 1'}], + buildResult: { + code: 0, + stdout: [{text: 'Binary compilation successful'}], + stderr: [], + }, + }; + }); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); // Compilation succeeded + expect(result.execResult).toBeDefined(); + expect(result.execResult?.didExecute).toBe(true); + expect(result.execResult?.code).toBe(1); // Execution failed + expect(result.execResult?.stderr).toEqual([{text: 'Execution failed with return code 1'}]); + }); + + it('should handle compilation failures for execution', async () => { + const source = 'int main() { error_function(); return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + execute: true, // Request execution + }); + + // Mock handleExecution to simulate binary compilation failure + vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { + return { + code: -1, + didExecute: false, + stdout: [], + stderr: [{text: 'Build failed'}], + buildResult: { + code: 1, + stdout: [], + stderr: [{text: 'error: use of undeclared identifier "error_function"'}], + }, + }; + }); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); // Assembly compilation succeeded + expect(result.execResult).toBeDefined(); + expect(result.execResult?.didExecute).toBe(false); + expect(result.execResult?.buildResult?.code).toBe(1); + expect(result.execResult?.buildResult?.stderr).toEqual([ + {text: 'error: use of undeclared identifier "error_function"'}, + ]); + }); + + it('should handle timeouts during execution', async () => { + const source = 'int main() { while(1); return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + execute: true, // Request execution + }); + + // Mock handleExecution to simulate execution timeout + vi.spyOn(compiler, 'handleExecution' as any).mockImplementation(async () => { + return { + code: -1, + didExecute: true, + stdout: [], + stderr: [{text: 'Execution time out'}], + timedOut: true, + buildResult: { + code: 0, + stdout: [{text: 'Binary compilation successful'}], + stderr: [], + }, + }; + }); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); // Compilation succeeded + expect(result.execResult).toBeDefined(); + expect(result.execResult?.didExecute).toBe(true); + expect(result.execResult?.timedOut).toBe(true); + expect(result.execResult?.stderr).toEqual([{text: 'Execution time out'}]); + }); + + it('should not execute when compiler does not support execution', async () => { + // Create a new compiler that doesn't support execution + const noExecCompilerInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + supportsExecute: false, // No execution support + supportsBinary: true, + }); + + const noExecCompiler = new BaseCompiler(noExecCompilerInfo, ce); + vi.spyOn(noExecCompiler, 'exec').mockImplementation(async () => { + return { + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }; + }); + + // Mock handleExecution to indicate compiler doesn't support execution + vi.spyOn(noExecCompiler, 'handleExecution' as any).mockImplementation(async () => { + return { + code: -1, + didExecute: false, + stdout: [], + stderr: [{text: 'Compiler does not support execution'}], + buildResult: { + code: 0, + stdout: [], + stderr: [], + }, + }; + }); + + const source = 'int main() { return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({ + execute: true, // Request execution despite no support + }); + + const result = await noExecCompiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); + expect(result.execResult).toBeDefined(); + expect(result.execResult?.didExecute).toBe(false); + expect(result.execResult?.stderr).toEqual([{text: 'Compiler does not support execution'}]); + }); +}); + +// ====================================================================== +// Error handling tests +// ====================================================================== +describe('BaseCompiler error handling', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let execSpy: any; + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + languages, + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + ce.findBadOptions = vi.fn().mockReturnValue([]); + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + }); + + compiler = new BaseCompiler(compilationInfo, ce); + execSpy = vi.spyOn(compiler, 'exec'); + + // Mock filesystem operations + vi.spyOn(fs, 'writeFile').mockImplementation(async () => {}); + vi.spyOn(fs, 'readFile').mockResolvedValue('Assembly output'); + + // Mock check source method to prevent validation issues + vi.spyOn(compiler, 'checkSource').mockReturnValue(null); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + it('should handle compilation timeouts properly', async () => { + // Simulate a timeout during compilation + execSpy.mockResolvedValue({ + code: -1, + stdout: '', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: true, + truncated: false, + }); + + const source = 'int main() { return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(-1); + expect(result.timedOut).toBe(true); + // The actual message might be different, but we know it should indicate compilation failure + expect(result.asm[0].text).toContain(''); + }); + + it('should handle output truncation', async () => { + // Simulate truncated output + execSpy.mockResolvedValue({ + code: 0, + stdout: 'Truncated stdout', + stderr: 'Truncated stderr', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: true, + }); + + const source = 'int main() { return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); + expect(result.truncated).toBe(true); + // The actual assembly text may vary in different environments + expect(result.stdout).toEqual([{text: 'Truncated stdout'}]); + expect(result.stderr).toEqual([{text: 'Truncated stderr'}]); + }); + + it('should handle corrupted output files', async () => { + // Successful compilation + execSpy.mockResolvedValue({ + code: 0, + stdout: 'Compilation successful', + stderr: '', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + // But readFile throws an error + vi.spyOn(fs, 'readFile').mockRejectedValue(new Error('Could not read output file')); + + const source = 'int main() { return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(0); // Compilation succeeded + // The actual error message may contain the specific error details + expect(result.asm[0].text).toContain('<'); + }); + + it('should handle source validation', async () => { + // Mock the checkSource method for testing + vi.spyOn(compiler, 'checkSource').mockImplementation((source: string) => { + if (source.includes('')) { + return ':1:1: no absolute or relative includes please'; + } + return null; + }); + + // Create a simple test case for source validation + const validSource = '#include \nint main() { return 0; }'; + const invalidSource = '#include '; + + // For valid source, checkSource should return null + expect(compiler.checkSource(validSource)).toBeNull(); + + // For invalid source with absolute path in include, it should return an error message + const result = compiler.checkSource(invalidSource); + expect(result).not.toBeNull(); + expect(result).toContain('no absolute or relative includes'); + }); + + it('should handle compiler failures with memory safety errors', async () => { + execSpy.mockResolvedValue({ + code: 139, // SIGSEGV exit code + stdout: '', + stderr: 'Segmentation fault', + okToCache: true, + filenameTransform: (x: string) => x, + execTime: 0, + timedOut: false, + truncated: false, + }); + + const source = 'int main() { return 0; }'; + const options = ['']; + const filters: ParseFiltersAndOutputOptions = makeFakeParseFiltersAndOutputOptions({}); + + const result = await compiler.compile( + source, + options, + {}, + filters, + BypassCache.None, + [], + {}, + [], + [] as FiledataPair[], + ); + + expect(result.code).toBe(139); + expect(result.asm).toEqual([{text: '', source: null, labels: []}]); + expect(result.stderr).toEqual([{text: 'Segmentation fault'}]); + }); +}); + +// ====================================================================== +// Path handling, options, and compilation settings +// ====================================================================== +describe('BaseCompiler utilities', () => { + let ce: CompilationEnvironment; + let compiler: BaseCompiler; + let tempDir: string; + + beforeEach(async () => { + ce = makeCompilationEnvironment({ + props: { + compileTimeoutMs: 7000, + binaryExecTimeoutMs: 2000, + }, + }); + + ce.findBadOptions = vi.fn().mockReturnValue([]); + + tempDir = await newTempDir(); + + const compilationInfo = makeFakeCompilerInfo({ + exe: '/fake/compiler/path', + remote: undefined, + lang: 'c++', + options: '-Wall -O2', + }); + + compiler = new BaseCompiler(compilationInfo, ce); + }); + + afterEach(async () => { + vi.restoreAllMocks(); + }); + + describe('getExtraFilepath', () => { + it('should join paths correctly', () => { + // Skip this test on Windows as paths are handled differently + if (process.platform === 'win32') { + return; + } + + // Access the protected method using a type assertion + const extraFilepath = (compiler as any).getExtraFilepath('/tmp/base/dir', 'file.txt'); + + expect(extraFilepath).toBe('/tmp/base/dir/file.txt'); + }); + + it('should normalize paths', () => { + // Skip this test on Windows as paths are handled differently + if (process.platform === 'win32') { + return; + } + + // Access the protected method using a type assertion + const extraFilepath = (compiler as any).getExtraFilepath('/tmp/base/dir', './subdir/../file.txt'); + + expect(extraFilepath).toBe('/tmp/base/dir/file.txt'); + }); + + it('should reject paths with parent directory traversal', () => { + // Access the protected method using a type assertion + expect(() => (compiler as any).getExtraFilepath('/tmp/base/dir', '../file.txt')).toThrow(Error); + expect(() => (compiler as any).getExtraFilepath('/tmp/base/dir', 'subdir/../../file.txt')).toThrow(Error); + }); + }); + + describe('optionsForFilter', () => { + // Using TypeScript's "any" assertion to access protected methods + const getOptions = (filter: any, output: string, userOptions?: string[]) => { + return (compiler as any).optionsForFilter(filter, output, userOptions); + }; + + it('should handle binary output option', () => { + const options = getOptions({binary: true}, 'output.out', []); + + // Should include -o output.out but not -S + expect(options).toContain('-o'); + expect(options).toContain('output.out'); + expect(options).not.toContain('-S'); + }); + + it('should handle assembly output option', () => { + const options = getOptions({binary: false}, 'output.s', []); + + // Should include -o output.s and -S + expect(options).toContain('-o'); + expect(options).toContain('output.s'); + expect(options).toContain('-S'); + }); + + it('should handle debug info option', () => { + const options = getOptions({binary: false}, 'output.s', []); + + // Should include -g + expect(options).toContain('-g'); + }); + + it('should accept user options but not include them directly', () => { + // The optionsForFilter method doesn't directly add userOptions to the return value + // That's handled later in prepareArguments and orderArguments methods + const options = getOptions({binary: false}, 'output.s', ['-O3', '-march=native']); + + // Should not contain user options yet - they're processed later in the compile chain + expect(options).not.toContain('-O3'); + expect(options).not.toContain('-march=native'); + + // Basic options should still be present + expect(options).toContain('-S'); + expect(options).toContain('-o'); + expect(options).toContain('output.s'); + }); + }); + + describe('writeAllFiles', () => { + it('should write source file to directory', async () => { + const fsWriteSpy = vi.spyOn(fs, 'writeFile').mockResolvedValue(); + + const source = 'int main() { return 0; }'; + + // Access the protected method using a type assertion + await (compiler as any).writeAllFiles(tempDir, source, [], {}); + + // Should have written the source file + // Access the protected compileFilename using a type assertion + expect(fsWriteSpy).toHaveBeenCalledWith(path.join(tempDir, (compiler as any).compileFilename), source); + }); + + it('should write multiple files', async () => { + const fsWriteSpy = vi.spyOn(fs, 'writeFile').mockResolvedValue(); + const outputFileSpy = vi.spyOn(utils, 'outputTextFile').mockResolvedValue(); + + const source = 'int main() { return 0; }'; + const extraFiles = [ + {filename: 'header.h', contents: '#pragma once'}, + {filename: 'impl.cpp', contents: '#include "header.h"'}, + ]; + + // Access the protected method using a type assertion + await (compiler as any).writeAllFiles(tempDir, source, extraFiles, {}); + + // Should have written the source file + // Access the protected compileFilename using a type assertion + expect(fsWriteSpy).toHaveBeenCalledWith(path.join(tempDir, (compiler as any).compileFilename), source); + + // Should have called outputTextFile for each extra file + expect(outputFileSpy).toHaveBeenCalledTimes(2); + + if (process.platform === 'win32') { + // On Windows, just check that both calls were made with the correct contents + // and don't check the exact paths (since they'll use backslashes) + expect(outputFileSpy.mock.calls[0][1]).toBe('#pragma once'); + expect(outputFileSpy.mock.calls[1][1]).toBe('#include "header.h"'); + + // Verify that the first call has header.h in the path + expect(outputFileSpy.mock.calls[0][0]).toContain('header.h'); + + // Verify that the second call has impl.cpp in the path + expect(outputFileSpy.mock.calls[1][0]).toContain('impl.cpp'); + } else { + // On non-Windows, check the exact paths with forward slashes + expect(outputFileSpy).toHaveBeenCalledWith(`${tempDir}/header.h`, '#pragma once'); + expect(outputFileSpy).toHaveBeenCalledWith(`${tempDir}/impl.cpp`, '#include "header.h"'); + } + }); + }); + + describe('findBadOptions', () => { + // We'll temporarily override the findBadOptions mock to test the real method + beforeEach(() => { + vi.restoreAllMocks(); + }); + + it('should check options against compiler environment', () => { + const ceSpy = vi.spyOn(ce, 'findBadOptions').mockReturnValue([]); + + const result = compiler.checkOptions(['-Wall', '-O2']); + + expect(ceSpy).toHaveBeenCalledWith(['-Wall', '-O2']); + expect(result).toBeNull(); + }); + + it('should return error message for bad options', () => { + vi.spyOn(ce, 'findBadOptions').mockReturnValue(['-badoption']); + + const result = compiler.checkOptions(['-Wall', '-badoption']); + + expect(result).toBe('Bad options: -badoption'); + }); + }); +}); + +// ====================================================================== +// Integration tests for compilation and execution options handling +// ====================================================================== +describe('Compiler execution options', () => { let ce: CompilationEnvironment; let compiler: BaseCompiler; - // let compilerNoExec: BaseCompiler; let win32compiler: Win32Compiler; const executingCompilerInfo = makeFakeCompilerInfo({ @@ -175,7 +1343,6 @@ describe('Compiler execution', () => { ce = makeCompilationEnvironment({languages}); compiler = new BaseCompiler(executingCompilerInfo, ce); win32compiler = new Win32Compiler(win32CompilerInfo, ce); - // compilerNoExec = new BaseCompiler(noExecuteSupportCompilerInfo, ce); }); it('basecompiler should handle spaces in options correctly', () => { @@ -288,339 +1455,6 @@ describe('Compiler execution', () => { expect(execOptions.env).not.toHaveProperty('ABC$#%@6@5'); }); - // it('should compile', async () => { - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output file', - // { - // code: 0, - // okToCache: true, - // stdout: 'stdout', - // stderr: 'stderr', - // }, - // undefined, - // ); - // const result = await compiler.compile('source', 'options', {}, {}, false, [], {}, [], undefined); - // result.code.should.equal(0); - // result.compilationOptions.should.contain('options'); - // result.compilationOptions.should.contain(result.inputFilename); - // result.okToCache.should.be.true; - // result.asm.should.deep.equal([{source: null, text: 'This is the output file', labels: []}]); - // result.stdout.should.deep.equal([{text: 'stdout'}]); - // result.stderr.should.deep.equal([{text: 'stderr'}]); - // result.popularArguments.should.deep.equal({}); - // result.tools.should.deep.equal([]); - // execStub.called.should.be.true; - // }); - // - // it('should handle compilation failures', async () => { - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output file', - // { - // code: 1, - // okToCache: true, - // stdout: '', - // stderr: 'oh noes', - // }, - // undefined, - // ); - // const result = await compiler.compile('source', 'options', {}, {}, false, [], {}, [], undefined); - // result.code.should.equal(1); - // result.asm.should.deep.equal([{labels: [], source: null, text: ''}]); - // }); - // - // it('should cache results (when asked)', async () => { - // const ceMock = mock(ce); - // const fakeExecResults = { - // code: 0, - // okToCache: true, - // stdout: 'stdout', - // stderr: 'stderr', - // }; - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec(execStub, compiler, 'This is the output file', fakeExecResults, undefined); - // const source = 'Some cacheable source'; - // const options = 'Some cacheable options'; - // ceMock - // .expects('cachePut') - // .withArgs( - // match({source, options}), - // match({ - // ...fakeExecResults, - // stdout: [{text: 'stdout'}], - // stderr: [{text: 'stderr'}], - // }), - // ) - // .resolves(); - // const uncachedResult = await compiler.compile(source, options, {}, {}, false, [], {}, [], undefined); - // uncachedResult.code.should.equal(0); - // ceMock.verify(); - // }); - // - // it('should not cache results (when not asked)', async () => { - // const ceMock = mock(ce); - // const fakeExecResults = { - // code: 0, - // okToCache: false, - // stdout: 'stdout', - // stderr: 'stderr', - // }; - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec(execStub, compiler, 'This is the output file', fakeExecResults, undefined); - // ceMock.expects('cachePut').never(); - // const source = 'Some cacheable source'; - // const options = 'Some cacheable options'; - // const uncachedResult = await compiler.compile(source, options, {}, {}, false, [], {}, [], undefined); - // uncachedResult.code.should.equal(0); - // ceMock.verify(); - // }); - // - // it('should read from the cache (when asked)', async () => { - // const ceMock = mock(ce); - // const source = 'Some previously cached source'; - // const options = 'Some previously cached options'; - // ceMock.expects('cacheGet').withArgs(match({source, options})).resolves({code: 123}); - // const cachedResult = await compiler.compile(source, options, {}, {}, false, [], {}, [], undefined); - // cachedResult.code.should.equal(123); - // ceMock.verify(); - // }); - // - // it('should note read from the cache (when bypassed)', async () => { - // const ceMock = mock(ce); - // const fakeExecResults = { - // code: 0, - // okToCache: true, - // stdout: 'stdout', - // stderr: 'stderr', - // }; - // const source = 'Some previously cached source'; - // const options = 'Some previously cached options'; - // ceMock.expects('cacheGet').never(); - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec(execStub, compiler, 'This is the output file', fakeExecResults, undefined); - // const uncachedResult = await compiler.compile(source, options, {}, {}, true, [], {}, [], undefined); - // uncachedResult.code.should.equal(0); - // ceMock.verify(); - // }); - // - // it('should execute', async () => { - // const execMock = mock(exec); - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output asm file', - // { - // code: 0, - // okToCache: true, - // stdout: 'asm stdout', - // stderr: 'asm stderr', - // }, - // 0, - // ); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output binary file', - // { - // code: 0, - // okToCache: true, - // stdout: 'binary stdout', - // stderr: 'binary stderr', - // }, - // 1, - // ); - // execMock.expects('sandbox').withArgs(match.string, match.array, match.object).resolves({ - // code: 0, - // stdout: 'exec stdout', - // stderr: 'exec stderr', - // }); - // const result = await compiler.compile('source', 'options', {}, {execute: true}, false, [], {}, [], undefined); - // result.code.should.equal(0); - // result.execResult.didExecute.should.be.true; - // result.stdout.should.deep.equal([{text: 'asm stdout'}]); - // result.execResult.stdout.should.deep.equal([{text: 'exec stdout'}]); - // result.execResult.buildResult.stdout.should.deep.equal([{text: 'binary stdout'}]); - // result.stderr.should.deep.equal([{text: 'asm stderr'}]); - // result.execResult.stderr.should.deep.equal([{text: 'exec stderr'}]); - // result.execResult.buildResult.stderr.should.deep.equal([{text: 'binary stderr'}]); - // execMock.verify(); - // }); - // - // it('should execute with an execution wrapper', async () => { - // const executionWrapper = '/some/wrapper/script.sh'; - // (compiler as any).compiler.executionWrapper = executionWrapper; - // const execMock = mock(exec); - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output asm file', - // { - // code: 0, - // okToCache: true, - // stdout: 'asm stdout', - // stderr: 'asm stderr', - // }, - // 0, - // ); - // stubOutCallToExec( - // execStub, - // compiler, - // 'This is the output binary file', - // { - // code: 0, - // okToCache: true, - // stdout: 'binary stdout', - // stderr: 'binary stderr', - // }, - // 1, - // ); - // execMock.expects('sandbox').withArgs(executionWrapper, match.array, match.object).resolves({ - // code: 0, - // stdout: 'exec stdout', - // stderr: 'exec stderr', - // }); - // await compiler.compile('source', 'options', {}, {execute: true}, false, [], {}, [], undefined); - // execMock.verify(); - // }); - // - // it('should not execute where not supported', async () => { - // const execMock = mock(exec); - // const execStub = stub(compilerNoExec, 'exec'); - // stubOutCallToExec( - // execStub, - // compilerNoExec, - // 'This is the output asm file', - // { - // code: 0, - // okToCache: true, - // stdout: 'asm stdout', - // stderr: 'asm stderr', - // }, - // 0, - // ); - // stubOutCallToExec( - // execStub, - // compilerNoExec, - // 'This is the output binary file', - // { - // code: 0, - // okToCache: true, - // stdout: 'binary stdout', - // stderr: 'binary stderr', - // }, - // 1, - // ); - // const result = await compilerNoExec.compile( - // 'source', - // 'options', - // {}, - // {execute: true}, - // false, - // [], - // {}, - // [], - // undefined, - // ); - // result.code.should.equal(0); - // result.execResult.didExecute.should.be.false; - // result.stdout.should.deep.equal([{text: 'asm stdout'}]); - // result.execResult.stdout.should.deep.equal([]); - // result.execResult.buildResult.stdout.should.deep.equal([{text: 'binary stdout'}]); - // result.stderr.should.deep.equal([{text: 'asm stderr'}]); - // result.execResult.stderr.should.deep.equal([{text: 'Compiler does not support execution'}]); - // result.execResult.buildResult.stderr.should.deep.equal([{text: 'binary stderr'}]); - // execMock.verify(); - // }); - // - // it('should demangle', async () => { - // const withDemangler = {...noExecuteSupportCompilerInfo, demangler: 'demangler-exe', demanglerType: 'cpp'}; - // const compiler = new BaseCompiler(withDemangler, ce); - // const execStub = stub(compiler, 'exec'); - // stubOutCallToExec( - // execStub, - // compiler, - // 'someMangledSymbol:\n', - // { - // code: 0, - // okToCache: true, - // stdout: 'stdout', - // stderr: 'stderr', - // }, - // undefined, - // ); - // execStub.onCall(1).callsFake((demangler, args, options) => { - // demangler.should.equal('demangler-exe'); - // options.input.should.equal('someMangledSymbol'); - // return Promise.resolve({ - // code: 0, - // filenameTransform: (x: any) => x, - // stdout: 'someDemangledSymbol\n', - // stderr: '', - // }); - // }); - // - // const result = await compiler.compile('source', 'options', {}, {demangle: true}, false, [], {}, [], undefined); - // result.code.should.equal(0); - // result.asm.should.deep.equal([{source: null, labels: [], text: 'someDemangledSymbol:'}]); - // // TODO all with demangle: false - // }); - // - // async function objdumpTest(type, expectedArgs) { - // const withObjdumper = { - // ...noExecuteSupportCompilerInfo, - // objdumper: 'objdump-exe', - // objdumperType: type, - // }; - // const compiler = new BaseCompiler(withObjdumper, ce); - // const execStub = stub(compiler, 'exec'); - // execStub.onCall(0).callsFake((objdumper, args, options) => { - // objdumper.should.equal('objdump-exe'); - // args.should.deep.equal(expectedArgs); - // options.maxOutput.should.equal(123456); - // return Promise.resolve({ - // code: 0, - // filenameTransform: x => x, - // stdout: '', - // stderr: '', - // }); - // }); - // compiler.supportsObjdump().should.be.true; - // const result = await compiler.objdump( - // 'output', - // {}, - // 123456, - // true, - // true, - // false, - // false, - // makeFakeParseFiltersAndOutputOptions({}), - // ); - // result.asm.should.deep.equal(''); - // } - - // it('should run default objdump properly', async () => { - // return objdumpTest('default', ['-d', 'output', '-l', '--insn-width=16', '-C', '-M', 'intel']); - // }); - // - // it('should run binutils objdump properly', async () => { - // return objdumpTest('binutils', ['-d', 'output', '-l', '--insn-width=16', '-C', '-M', 'intel']); - // }); - // - // it('should run ELF Tool Chain objdump properly', async () => { - // return objdumpTest('elftoolchain', ['-d', 'output', '-l', '-C', '-M', 'intel']); - // }); - // - // it('should run LLVM objdump properly', async () => { - // return objdumpTest('llvm', ['-d', 'output', '-l', '-C', '--x86-asm-syntax=intel']); - // }); - it('should run process llvm opt output', async () => { const test = `--- !Missed Pass: inline @@ -734,6 +1568,9 @@ Args: [] }); }); +// ====================================================================== +// Environment and path handling tests +// ====================================================================== describe('getDefaultExecOptions', () => { let ce: CompilationEnvironment; @@ -770,6 +1607,9 @@ describe('getDefaultExecOptions', () => { }); }); +// ====================================================================== +// Target and architecture handling +// ====================================================================== describe('Target hints', () => { let ce: CompilationEnvironment; @@ -806,6 +1646,9 @@ describe('Target hints', () => { }); }); +// ====================================================================== +// Language-specific compiler overrides +// ====================================================================== describe('Rust overrides', () => { let ce: CompilationEnvironment; const executingCompilerInfo = makeFakeCompilerInfo({ diff --git a/test/base-compiler-utils-tests.ts b/test/base-compiler-utils-tests.ts deleted file mode 100644 index d6534877c..000000000 --- a/test/base-compiler-utils-tests.ts +++ /dev/null @@ -1,220 +0,0 @@ -// Copyright (c) 2025, 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 fs from 'node:fs/promises'; -import path from 'node:path'; -import {afterEach, beforeEach, describe, expect, it, vi} from 'vitest'; - -import {BaseCompiler} from '../lib/base-compiler.js'; -import {CompilationEnvironment} from '../lib/compilation-env.js'; -import * as utils from '../lib/utils.js'; -import {makeCompilationEnvironment, makeFakeCompilerInfo, newTempDir} from './utils.js'; - -describe('BaseCompiler utilities', () => { - let ce: CompilationEnvironment; - let compiler: BaseCompiler; - let tempDir: string; - - beforeEach(async () => { - ce = makeCompilationEnvironment({ - props: { - compileTimeoutMs: 7000, - binaryExecTimeoutMs: 2000, - }, - }); - - ce.findBadOptions = vi.fn().mockReturnValue([]); - - tempDir = await newTempDir(); - - const compilationInfo = makeFakeCompilerInfo({ - exe: '/fake/compiler/path', - remote: undefined, - lang: 'c++', - options: '-Wall -O2', - }); - - compiler = new BaseCompiler(compilationInfo, ce); - }); - - afterEach(async () => { - vi.restoreAllMocks(); - }); - - describe('getExtraFilepath', () => { - it('should join paths correctly', () => { - // Skip this test on Windows as paths are handled differently - if (process.platform === 'win32') { - return; - } - - // Access the protected method using a type assertion - const extraFilepath = (compiler as any).getExtraFilepath('/tmp/base/dir', 'file.txt'); - - expect(extraFilepath).toBe('/tmp/base/dir/file.txt'); - }); - - it('should normalize paths', () => { - // Skip this test on Windows as paths are handled differently - if (process.platform === 'win32') { - return; - } - - // Access the protected method using a type assertion - const extraFilepath = (compiler as any).getExtraFilepath('/tmp/base/dir', './subdir/../file.txt'); - - expect(extraFilepath).toBe('/tmp/base/dir/file.txt'); - }); - - it('should reject paths with parent directory traversal', () => { - // Access the protected method using a type assertion - expect(() => (compiler as any).getExtraFilepath('/tmp/base/dir', '../file.txt')).toThrow(Error); - expect(() => (compiler as any).getExtraFilepath('/tmp/base/dir', 'subdir/../../file.txt')).toThrow(Error); - }); - }); - - describe('optionsForFilter', () => { - // Using TypeScript's "any" assertion to access protected methods - const getOptions = (filter: any, output: string, userOptions?: string[]) => { - return (compiler as any).optionsForFilter(filter, output, userOptions); - }; - - it('should handle binary output option', () => { - const options = getOptions({binary: true}, 'output.out', []); - - // Should include -o output.out but not -S - expect(options).toContain('-o'); - expect(options).toContain('output.out'); - expect(options).not.toContain('-S'); - }); - - it('should handle assembly output option', () => { - const options = getOptions({binary: false}, 'output.s', []); - - // Should include -o output.s and -S - expect(options).toContain('-o'); - expect(options).toContain('output.s'); - expect(options).toContain('-S'); - }); - - it('should handle debug info option', () => { - const options = getOptions({binary: false}, 'output.s', []); - - // Should include -g - expect(options).toContain('-g'); - }); - - it('should accept user options but not include them directly', () => { - // The optionsForFilter method doesn't directly add userOptions to the return value - // That's handled later in prepareArguments and orderArguments methods - const options = getOptions({binary: false}, 'output.s', ['-O3', '-march=native']); - - // Should not contain user options yet - they're processed later in the compile chain - expect(options).not.toContain('-O3'); - expect(options).not.toContain('-march=native'); - - // Basic options should still be present - expect(options).toContain('-S'); - expect(options).toContain('-o'); - expect(options).toContain('output.s'); - }); - }); - - describe('writeAllFiles', () => { - it('should write source file to directory', async () => { - const fsWriteSpy = vi.spyOn(fs, 'writeFile').mockResolvedValue(); - - const source = 'int main() { return 0; }'; - - // Access the protected method using a type assertion - await (compiler as any).writeAllFiles(tempDir, source, [], {}); - - // Should have written the source file - // Access the protected compileFilename using a type assertion - expect(fsWriteSpy).toHaveBeenCalledWith(path.join(tempDir, (compiler as any).compileFilename), source); - }); - - it('should write multiple files', async () => { - const fsWriteSpy = vi.spyOn(fs, 'writeFile').mockResolvedValue(); - const outputFileSpy = vi.spyOn(utils, 'outputTextFile').mockResolvedValue(); - - const source = 'int main() { return 0; }'; - const extraFiles = [ - {filename: 'header.h', contents: '#pragma once'}, - {filename: 'impl.cpp', contents: '#include "header.h"'}, - ]; - - // Access the protected method using a type assertion - await (compiler as any).writeAllFiles(tempDir, source, extraFiles, {}); - - // Should have written the source file - // Access the protected compileFilename using a type assertion - expect(fsWriteSpy).toHaveBeenCalledWith(path.join(tempDir, (compiler as any).compileFilename), source); - - // Should have called outputTextFile for each extra file - expect(outputFileSpy).toHaveBeenCalledTimes(2); - - if (process.platform === 'win32') { - // On Windows, just check that both calls were made with the correct contents - // and don't check the exact paths (since they'll use backslashes) - expect(outputFileSpy.mock.calls[0][1]).toBe('#pragma once'); - expect(outputFileSpy.mock.calls[1][1]).toBe('#include "header.h"'); - - // Verify that the first call has header.h in the path - expect(outputFileSpy.mock.calls[0][0]).toContain('header.h'); - - // Verify that the second call has impl.cpp in the path - expect(outputFileSpy.mock.calls[1][0]).toContain('impl.cpp'); - } else { - // On non-Windows, check the exact paths with forward slashes - expect(outputFileSpy).toHaveBeenCalledWith(`${tempDir}/header.h`, '#pragma once'); - expect(outputFileSpy).toHaveBeenCalledWith(`${tempDir}/impl.cpp`, '#include "header.h"'); - } - }); - }); - - describe('findBadOptions', () => { - // We'll temporarily override the findBadOptions mock to test the real method - beforeEach(() => { - vi.restoreAllMocks(); - }); - - it('should check options against compiler environment', () => { - const ceSpy = vi.spyOn(ce, 'findBadOptions').mockReturnValue([]); - - const result = compiler.checkOptions(['-Wall', '-O2']); - - expect(ceSpy).toHaveBeenCalledWith(['-Wall', '-O2']); - expect(result).toBeNull(); - }); - - it('should return error message for bad options', () => { - vi.spyOn(ce, 'findBadOptions').mockReturnValue(['-badoption']); - - const result = compiler.checkOptions(['-Wall', '-badoption']); - - expect(result).toBe('Bad options: -badoption'); - }); - }); -});