Laszlo Csomor | d2cba29 | 2017-02-10 10:38:14 +0000 | [diff] [blame] | 1 | // Copyright 2016 The Bazel Authors. All rights reserved. |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Laszlo Csomor | a63d816 | 2019-05-21 04:35:07 -0700 | [diff] [blame] | 14 | |
| 15 | #ifndef WIN32_LEAN_AND_MEAN |
| 16 | #define WIN32_LEAN_AND_MEAN |
| 17 | #endif |
Laszlo Csomor | d2cba29 | 2017-02-10 10:38:14 +0000 | [diff] [blame] | 18 | #include <windows.h> |
| 19 | |
| 20 | #include <string> |
| 21 | |
ccalvarin | 61b4075 | 2018-03-26 12:32:39 -0700 | [diff] [blame] | 22 | #include "googletest/include/gtest/gtest.h" |
Laszlo Csomor | d2cba29 | 2017-02-10 10:38:14 +0000 | [diff] [blame] | 23 | #include "src/test/cpp/util/windows_test_util.h" |
| 24 | |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 25 | #if !defined(_WIN32) && !defined(__CYGWIN__) |
Laszlo Csomor | d2cba29 | 2017-02-10 10:38:14 +0000 | [diff] [blame] | 26 | #error("This test should only be run on Windows") |
Loo Rong Jie | 4022bac | 2018-06-11 02:04:52 -0700 | [diff] [blame] | 27 | #endif // !defined(_WIN32) && !defined(__CYGWIN__) |
Laszlo Csomor | d2cba29 | 2017-02-10 10:38:14 +0000 | [diff] [blame] | 28 | |
| 29 | namespace blaze_util { |
| 30 | |
| 31 | using std::wstring; |
| 32 | |
| 33 | class WindowsTestUtilTest : public ::testing::Test { |
| 34 | public: |
| 35 | void SetUp() override { ::CreateDirectoryW(GetTestTmpDirW().c_str(), NULL); } |
| 36 | void TearDown() override { DeleteAllUnder(GetTestTmpDirW()); } |
| 37 | }; |
| 38 | |
| 39 | TEST_F(WindowsTestUtilTest, TestGetTestTempDirW) { |
| 40 | wstring actual = GetTestTmpDirW(); |
| 41 | ASSERT_EQ(actual.find(L":\\"), 1); |
| 42 | ASSERT_EQ(actual.find(L"/"), wstring::npos); |
| 43 | } |
| 44 | |
| 45 | TEST_F(WindowsTestUtilTest, TestCreateDummyFile) { |
| 46 | wstring wtemp = GetTestTmpDirW(); |
| 47 | EXPECT_FALSE(wtemp.empty()); |
| 48 | wstring file1 = wstring(L"\\\\?\\") + wtemp + L"\\file1.txt"; |
| 49 | ASSERT_TRUE(CreateDummyFile(file1)); |
| 50 | DWORD attr = ::GetFileAttributesW(file1.c_str()); |
| 51 | ASSERT_NE(attr, INVALID_FILE_ATTRIBUTES); |
| 52 | } |
| 53 | |
| 54 | TEST_F(WindowsTestUtilTest, TestDeleteAllUnder) { |
| 55 | wstring wtemp = GetTestTmpDirW(); |
| 56 | EXPECT_FALSE(wtemp.empty()); |
| 57 | wstring dir1 = wstring(L"\\\\?\\") + wtemp + L"\\dir1"; |
| 58 | EXPECT_TRUE(::CreateDirectoryW(dir1.c_str(), NULL)); |
| 59 | EXPECT_TRUE(CreateDummyFile(dir1 + L"\\file1.txt")); |
| 60 | wstring dir2 = dir1 + L"\\dir2"; |
| 61 | EXPECT_TRUE(::CreateDirectoryW(dir2.c_str(), NULL)); |
| 62 | EXPECT_TRUE(CreateDummyFile(dir2 + L"\\file2.txt")); |
| 63 | ASSERT_TRUE(DeleteAllUnder(dir1)); |
| 64 | } |
| 65 | |
| 66 | } // namespace blaze_util |