github.com/databricks/cli@v0.203.0/internal/acc/debug.go (about)

     1  package acc
     2  
     3  import (
     4  	"encoding/json"
     5  	"os"
     6  	"path"
     7  	"path/filepath"
     8  	"testing"
     9  )
    10  
    11  // Detects if test is run from "debug test" feature in VS Code.
    12  func isInDebug() bool {
    13  	ex, _ := os.Executable()
    14  	return path.Base(ex) == "__debug_bin"
    15  }
    16  
    17  // Loads debug environment from ~/.databricks/debug-env.json.
    18  func loadDebugEnvIfRunFromIDE(t *testing.T, key string) {
    19  	if !isInDebug() {
    20  		return
    21  	}
    22  	home, err := os.UserHomeDir()
    23  	if err != nil {
    24  		t.Fatalf("cannot find user home: %s", err)
    25  	}
    26  	raw, err := os.ReadFile(filepath.Join(home, ".databricks/debug-env.json"))
    27  	if err != nil {
    28  		t.Fatalf("cannot load ~/.databricks/debug-env.json: %s", err)
    29  	}
    30  	var conf map[string]map[string]string
    31  	err = json.Unmarshal(raw, &conf)
    32  	if err != nil {
    33  		t.Fatalf("cannot parse ~/.databricks/debug-env.json: %s", err)
    34  	}
    35  	vars, ok := conf[key]
    36  	if !ok {
    37  		t.Fatalf("~/.databricks/debug-env.json#%s not configured", key)
    38  	}
    39  	for k, v := range vars {
    40  		os.Setenv(k, v)
    41  	}
    42  }