Home > Blockchain >  Allow test result to depend upon installation of external tool
Allow test result to depend upon installation of external tool

Time:11-29

I am creating a Golang project, and doing a pretty good job of adding tests in step with the features. The general idea is that I perform a "semantic diff" between files and git branches. For the newest feature, the behavior depends upon whether an external tool (tree-sitter-cli) is installed, and upon which extra capabilities are installed.

If the external tool (or its optional grammars) are not installed, I expect different results from my internal function. However, both results are consistent with my tool (sdt) itself being correct. For example, here is a test I have modified from another programming language analyzed without tree-sitter:

func TestNoSemanticDiff(t *testing.T) {
    // Narrow the options to these test files
    opts := options
    opts.Source = file0.name
    opts.Destination = file1.name

    report := treesitter.Diff("", opts, config)

    if !strings.Contains(report, "| No semantic differences detected") {
        t.Fatalf("Failed to recognize semantic equivalence of %s and %s",
            opts.Source, opts.Destination)
    }
}

The tests for various supported languages are mostly the same in form. In this case, however, receiving the "report" of "| No available semantic analyzer for this format" would also be a correct value if the external tool is missing (or did not include that capability).

The thing is, I can run code to determine which value is expected; but that code would ideally be "run once at top of test" rather than re-check within each of many similar tests.

So what I'd ideally want is to first run some sort of setup/scaffolding, then have individual tests written something like:

if !treeSitterInstalled { // checked once at start of test file
    if !strings.Contains(report, "appropriate response") { ... }
} else if treeSitterNoLangSupport { // ditto, configured once
    if !strings.Contains(report, "this other thing") { ... }
} else {
    if !string.Contains(report, "the stuff where it's installed") { ... }
}

CodePudding user response:

In your test file you could simply use the init function to do the checking for the external dependency and set a variable inside that very same file, which can then be checked inside individual tests.

  • Related