| 14 | } |
| 15 | |
| 16 | func TestWithWd(t *testing.T) { |
| 17 | wd1, _ := os.Getwd() |
| 18 | tmpDir := NewTmpDir("gotify_withwd") |
| 19 | defer tmpDir.Clean() |
| 20 | var wd2 string |
| 21 | WithWd(tmpDir.Path(), func(origWd string) { |
| 22 | assert.Equal(t, wd1, origWd) |
| 23 | wd2, _ = os.Getwd() |
| 24 | }) |
| 25 | wd3, _ := os.Getwd() |
| 26 | assert.Equal(t, wd1, wd3) |
| 27 | assert.Equal(t, tmpDir.Path(), wd2) |
| 28 | assert.Nil(t, os.RemoveAll(tmpDir.Path())) |
| 29 | |
| 30 | assert.Panics(t, func() { |
| 31 | WithWd("non_exist", func(string) {}) |
| 32 | }) |
| 33 | |
| 34 | assert.Nil(t, os.Mkdir(tmpDir.Path(), 0o644)) |
| 35 | if os.Getuid() != 0 { // root is not subject to this check |
| 36 | assert.Panics(t, func() { |
| 37 | WithWd(tmpDir.Path(), func(string) {}) |
| 38 | }) |
| 39 | } |
| 40 | assert.Nil(t, os.Remove(tmpDir.Path())) |
| 41 | |
| 42 | assert.Nil(t, os.Mkdir(tmpDir.Path(), 0o755)) |
| 43 | assert.Panics(t, func() { |
| 44 | WithWd(tmpDir.Path(), func(string) { |
| 45 | assert.Nil(t, os.RemoveAll(tmpDir.Path())) |
| 46 | WithWd(".", func(string) {}) |
| 47 | }) |
| 48 | }) |
| 49 | } |