microsoft / microsoft/go-sqlcmd

How to determine if running in unittest framework

Open
#168 0 comments 0 reactions 1 assignee View on GitHub

@stuartpa is already working on this.

Since Nov 29, 2022.

housekeeping testing
Dominant language
Go
Stars
595
Forks
91
Avg merge
9h 35m
Merged PRs (30d)
1

Description

There is a place in the code we need to determine if we are running in a unit-test framework (to ensure the code doesn't call os.Exit(), which would shut down the whole testing process (instead we call panic, which just stop the current test).

What's the right way to determine if running in a unit-test framework

// CheckErr passes the error down to cobra.CheckErr (which is likely to call
// os.Exit(1) if err != nil. Although if running in the golang unit test framework
// we do not want to have os.Exit() called, as this exits the unit test runner
// process, and call panic instead so the call stack can be added to the unit test
// output.
func (c *Cmd) CheckErr(err error) {
// If we are in a unit test driver, then panic, otherwise pass down to cobra.CheckErr
if strings.HasSuffix(os.Args[0], ".test") || // are we in go test?
(len(os.Args) > 1 && os.Args[1] == "-test.v") { // are we in goland unittest?
if err != nil {
panic(err)
}
} else {

PR feedback - "this seems like a flimsy convention considering someone else could write a new test framework etc. Can these test frameworks not set some global state instead of relying on command line parsing?" - David

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.