TestMain is initially called by go test to initiate the testing. TestMain is also called during the tests to start rclone main in a fresh context (using exec.Command). The context is determined by setting/finding the environment variable RCLONE_TEST_MAIN
(m *testing.M)
| 21 | // TestMain is also called during the tests to start rclone main in a fresh context (using exec.Command). |
| 22 | // The context is determined by setting/finding the environment variable RCLONE_TEST_MAIN |
| 23 | func TestMain(m *testing.M) { |
| 24 | _, found := os.LookupEnv(rcloneTestMain) |
| 25 | if !found { |
| 26 | // started by Go test => execute tests |
| 27 | err := os.Setenv(rcloneTestMain, "true") |
| 28 | if err != nil { |
| 29 | fs.Fatalf(nil, "Unable to set %s: %s", rcloneTestMain, err.Error()) |
| 30 | } |
| 31 | os.Exit(m.Run()) |
| 32 | } else { |
| 33 | // started by func rcloneExecMain => call rclone main in cmdtest.go |
| 34 | err := os.Unsetenv(rcloneTestMain) |
| 35 | if err != nil { |
| 36 | fs.Fatalf(nil, "Unable to unset %s: %s", rcloneTestMain, err.Error()) |
| 37 | } |
| 38 | main() |
| 39 | } |
| 40 | } |
| 41 | |
| 42 | const rcloneTestMain = "RCLONE_TEST_MAIN" |
| 43 |