matepek / vscode-catch2-test-adapter

Catch2, Google Test and doctest Adapter for the VSCode
https://marketplace.visualstudio.com/items?itemName=matepek.vscode-catch2-test-adapter
MIT License
209 stars 52 forks source link

Allow running tests as a temporary executable #406

Closed nfogh closed 10 months ago

nfogh commented 11 months ago

I am using this extension together with TriggerTaskOnSave which allows me to build and run my unit tests pretty much continuously, getting instant feedback on any mistakes I do.

I have one problem on WIN32. While the tests are running, the build process will fail, as it cannot overwrite the currently executing test. Right now, I have a solution that copies the test executable to a temporary files by wrapping the execution in a script

            "executionWrapper": {
                "path": "C:${osPathSep}Python311${osPathSep}python.exe",
                "args": [
                    "RunTestInTempExecutable.py",
                    "${cmd} ${argsStr}"
                ]
            }

With the script:

import shutil
import os
import subprocess
import sys
import uuid

# This is awful, but it works
def cleanupString(s):
    return s.replace("\\\\", "\\").replace("\\\\", "\\").replace("/", "\\").replace("\"", "").replace("'", "")

def main():
    argvsplit = sys.argv[1].split(" ")
    testExecutable = os.path.abspath(cleanupString(argvsplit[0]))
    remainingArgs = cleanupString(" ".join(argvsplit[1:]) + " " + " ".join(sys.argv[2:]))

    tempTestExecutable = os.path.abspath("test-" + str(uuid.uuid1()) + ".exe")

    print("Copying " + testExecutable + " to " + tempTestExecutable)
    shutil.copyfile(testExecutable, tempTestExecutable)
    if os.path.exists(tempTestExecutable):
        cmdline = tempTestExecutable + " " + remainingArgs
        returnCode = os.system(cmdline)
        os.remove(tempTestExecutable)

        return returnCode
    else:
        return -1

if __name__ == "__main__":
    print("Forwarder")
    sys.exit(main())

Pardon the horrible code :)

It would be great if this could be somehow supported natively with a setting.

matepek commented 11 months ago

Interesting idea, let me think about it.

matepek commented 10 months ago

Added a new option in version 4.7

  • testMate.test.advancedExecutables - executableCloning: (experimental) If enabled it creates a copy of the test executable before listing or running the tests. NOTE: discovery (--help) still uses the original file.