Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions macaron.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,8 @@ var (
Env = DEV
envLock sync.Mutex

// Path of work directory.
// Root is the working directory at startup. It is empty if the directory
// could not be determined.
Root string

// Flash applies to current request.
Expand Down Expand Up @@ -309,11 +310,7 @@ func safeEnv() string {
func init() {
setENV(os.Getenv("MACARON_ENV"))

var err error
Root, err = os.Getwd()
if err != nil {
panic("error getting work directory: " + err.Error())
}
Root, _ = os.Getwd()
}

// SetConfig sets data sources for configuration.
Expand Down
31 changes: 31 additions & 0 deletions macaron_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,43 @@ import (
"net/http"
"net/http/httptest"
"os"
"os/exec"
"runtime"
"testing"
"time"

. "github.com/smartystreets/goconvey/convey"
)

func TestInitWithDeletedWorkingDirectory(t *testing.T) {
if runtime.GOOS != "linux" {
t.Skip("requires removing the working directory of a running process")
}
const childEnv = "MACARON_TEST_DELETED_CWD"
if os.Getenv(childEnv) == "1" {
if _, err := os.Getwd(); err == nil {
t.Fatal("expected os.Getwd to fail in the deleted directory")
}
if Root != "" {
t.Fatalf("Root = %q; want empty when os.Getwd fails", Root)
}
return
}

exe, err := os.Executable()
if err != nil {
t.Fatal(err)
}
dir := t.TempDir()
// Delete the directory after the shell enters it, then run package initialization again.
cmd := exec.Command("sh", "-c", `rmdir "$1" && exec "$2" -test.run '^TestInitWithDeletedWorkingDirectory$'`, "sh", dir, exe)
cmd.Dir = dir
cmd.Env = append(cmd.Environ(), childEnv+"=1")
if out, err := cmd.CombinedOutput(); err != nil {
t.Fatalf("test process failed to start from a deleted working directory: %v\n%s", err, out)
}
}

func Test_Version(t *testing.T) {
Convey("Get version", t, func() {
So(Version(), ShouldEqual, _VERSION)
Expand Down