1 // Copyright 2018 The Fuchsia Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 // Test main for the runtests-utils test on POSIX systems (e.g., Linux and
6 // MacOS).
7 
8 #include "runtests-utils-test-globals.h"
9 
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <unistd.h>
13 
14 #include <fbl/auto_call.h>
15 #include <runtests-utils/posix-run-test.h>
16 #include <unittest/unittest.h>
17 
18 namespace runtests {
19 
20 // Pointer to root of unique subdirectory of TMPDIR or /tmp.
21 static const fbl::String* TmpDirRoot = nullptr;
22 
23 const char kScriptShebang[32] = "#!/bin/sh\n\n";
24 const RunTestFn PlatformRunTest = &PosixRunTest;
25 
TestFsRoot()26 const char* TestFsRoot() {
27     if (TmpDirRoot == nullptr) {
28         char test_fs_template[256];
29         sprintf(test_fs_template, "%s/XXXXXX",
30                 getenv("TMPDIR") ? getenv("TMPDIR") : "/tmp");
31         TmpDirRoot = new fbl::String(mkdtemp(test_fs_template));
32     }
33     return TmpDirRoot->c_str();
34 }
35 
36 } // namespace runtests
37 
main(int argc,char ** argv)38 int main(int argc, char** argv) {
39     printf("\nRoot directory of the filesystem used for testing: %s\n",
40            runtests::TestFsRoot());
41 
42     auto auto_test_fs_clean_up = fbl::MakeAutoCall([&] {
43         // Since all subdirectories created will be scoped, at the end of the
44         // test TestFsRoot() should be empty.
45         remove(runtests::TestFsRoot());
46         delete runtests::TmpDirRoot;
47     });
48     return unittest_run_all_tests(argc, argv) ? EXIT_SUCCESS : EXIT_FAILURE;
49 }
50