1 | // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
2 | // for details. All rights reserved. Use of this source code is governed by a |
3 | // BSD-style license that can be found in the LICENSE file. |
4 | |
5 | #include "bin/directory.h" |
6 | #include "include/dart_api.h" |
7 | #include "platform/assert.h" |
8 | #include "vm/unit_test.h" |
9 | |
10 | namespace dart { |
11 | |
12 | VM_UNIT_TEST_CASE(DirectoryCurrentNoScope) { |
13 | char* current_dir = dart::bin::Directory::CurrentNoScope(); |
14 | EXPECT_NOTNULL(current_dir); |
15 | free(current_dir); |
16 | } |
17 | |
18 | TEST_CASE(DirectoryCurrent) { |
19 | const char* current = dart::bin::Directory::Current(NULL); |
20 | EXPECT_NOTNULL(current); |
21 | } |
22 | |
23 | TEST_CASE(DirectoryExists) { |
24 | const char* current = dart::bin::Directory::Current(NULL); |
25 | EXPECT_NOTNULL(current); |
26 | |
27 | dart::bin::Directory::ExistsResult r = |
28 | dart::bin::Directory::Exists(NULL, current); |
29 | EXPECT_EQ(dart::bin::Directory::EXISTS, r); |
30 | } |
31 | |
32 | TEST_CASE(DirectorySystemTemp) { |
33 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
34 | EXPECT_NOTNULL(system_temp); |
35 | } |
36 | |
37 | TEST_CASE(DirectorySystemTempExists) { |
38 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
39 | EXPECT_NOTNULL(system_temp); |
40 | |
41 | dart::bin::Directory::ExistsResult r = |
42 | dart::bin::Directory::Exists(NULL, system_temp); |
43 | EXPECT_EQ(dart::bin::Directory::EXISTS, r); |
44 | } |
45 | |
46 | TEST_CASE(DirectoryCreateTemp) { |
47 | const char* kTempPrefix = "test_prefix" ; |
48 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
49 | EXPECT_NOTNULL(system_temp); |
50 | |
51 | const char* temp_dir = dart::bin::Directory::CreateTemp(NULL, kTempPrefix); |
52 | EXPECT_NOTNULL(temp_dir); |
53 | |
54 | // Make sure temp_dir contains test_prefix. |
55 | EXPECT_NOTNULL(strstr(temp_dir, kTempPrefix)); |
56 | |
57 | // Cleanup. |
58 | EXPECT(dart::bin::Directory::Delete(NULL, temp_dir, false)); |
59 | } |
60 | |
61 | TEST_CASE(DirectorySetCurrent) { |
62 | const char* current = dart::bin::Directory::Current(NULL); |
63 | EXPECT_NOTNULL(current); |
64 | |
65 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
66 | EXPECT_NOTNULL(system_temp); |
67 | |
68 | EXPECT(dart::bin::Directory::SetCurrent(NULL, system_temp)); |
69 | |
70 | const char* new_current = dart::bin::Directory::Current(NULL); |
71 | EXPECT_NOTNULL(new_current); |
72 | |
73 | EXPECT_NOTNULL(strstr(new_current, system_temp)); |
74 | |
75 | EXPECT(dart::bin::Directory::SetCurrent(NULL, current)); |
76 | } |
77 | |
78 | TEST_CASE(DirectoryCreateDelete) { |
79 | const char* kTempDirName = "create_delete_test_name" ; |
80 | |
81 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
82 | EXPECT_NOTNULL(system_temp); |
83 | |
84 | const intptr_t name_len = |
85 | snprintf(NULL, 0, "%s/%s" , system_temp, kTempDirName); |
86 | ASSERT(name_len > 0); |
87 | char* name = new char[name_len + 1]; |
88 | snprintf(name, name_len + 1, "%s/%s" , system_temp, kTempDirName); |
89 | |
90 | // Make a directory. |
91 | EXPECT(dart::bin::Directory::Create(NULL, name)); |
92 | |
93 | // Make sure it exists. |
94 | dart::bin::Directory::ExistsResult r = |
95 | dart::bin::Directory::Exists(NULL, name); |
96 | EXPECT_EQ(dart::bin::Directory::EXISTS, r); |
97 | |
98 | // Cleanup. |
99 | EXPECT(dart::bin::Directory::Delete(NULL, name, false)); |
100 | delete[] name; |
101 | } |
102 | |
103 | TEST_CASE(DirectoryRename) { |
104 | const char* kTempDirName = "rename_test_name" ; |
105 | |
106 | const char* system_temp = dart::bin::Directory::SystemTemp(NULL); |
107 | EXPECT_NOTNULL(system_temp); |
108 | |
109 | const intptr_t name_len = |
110 | snprintf(NULL, 0, "%s/%s" , system_temp, kTempDirName); |
111 | ASSERT(name_len > 0); |
112 | char* name = new char[name_len + 1]; |
113 | snprintf(name, name_len + 1, "%s/%s" , system_temp, kTempDirName); |
114 | |
115 | // Make a directory. |
116 | EXPECT(dart::bin::Directory::Create(NULL, name)); |
117 | |
118 | // Make sure it exists. |
119 | dart::bin::Directory::ExistsResult r = |
120 | dart::bin::Directory::Exists(NULL, name); |
121 | EXPECT_EQ(dart::bin::Directory::EXISTS, r); |
122 | |
123 | const intptr_t new_name_len = |
124 | snprintf(NULL, 0, "%s/%snewname" , system_temp, kTempDirName); |
125 | ASSERT(new_name_len > 0); |
126 | char* new_name = new char[new_name_len + 1]; |
127 | snprintf(new_name, new_name_len + 1, "%s/%snewname" , system_temp, |
128 | kTempDirName); |
129 | |
130 | EXPECT(dart::bin::Directory::Rename(NULL, name, new_name)); |
131 | |
132 | r = dart::bin::Directory::Exists(NULL, new_name); |
133 | EXPECT_EQ(dart::bin::Directory::EXISTS, r); |
134 | |
135 | r = dart::bin::Directory::Exists(NULL, name); |
136 | EXPECT_EQ(dart::bin::Directory::DOES_NOT_EXIST, r); |
137 | |
138 | EXPECT(dart::bin::Directory::Delete(NULL, new_name, false)); |
139 | delete[] name; |
140 | delete[] new_name; |
141 | } |
142 | |
143 | } // namespace dart |
144 | |