github.com/jflude/taocp@v0.0.0-20240210234939-99f2a91af3c2/mix/sandbox_test.go (about)

     1  package mix
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  )
     7  
     8  func newSandbox(t *testing.T, tmpDir string) (*Computer, string) {
     9  	var ret string
    10  	var err error
    11  	if tmpDir == "" {
    12  		tmpDir, err = os.MkdirTemp("", "taocp-mix-test")
    13  		if err != nil {
    14  			t.Fatal("error:", err)
    15  		}
    16  		ret = tmpDir
    17  	}
    18  	if err = os.Chdir(tmpDir); err != nil {
    19  		t.Fatal("error:", err)
    20  	}
    21  	return NewComputer(), ret
    22  }
    23  
    24  func closeSandbox(t *testing.T, c *Computer, tmpDir string) {
    25  	if err := c.Shutdown(); err != nil {
    26  		t.Error("error:", err)
    27  	}
    28  	if tmpDir != "" {
    29  		if err := os.RemoveAll(tmpDir); err != nil {
    30  			t.Error("error:", err)
    31  		}
    32  	}
    33  }