blob: d8c9503b126a984e34144757b5891c26644c2436 [file] [log] [blame]
Laszlo Csomord2cba292017-02-10 10:38:14 +00001// 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 Csomora63d8162019-05-21 04:35:07 -070014
15#ifndef WIN32_LEAN_AND_MEAN
16#define WIN32_LEAN_AND_MEAN
17#endif
Laszlo Csomord2cba292017-02-10 10:38:14 +000018#include <windows.h>
19
20#include <string>
21
ccalvarin61b40752018-03-26 12:32:39 -070022#include "googletest/include/gtest/gtest.h"
Laszlo Csomord2cba292017-02-10 10:38:14 +000023#include "src/test/cpp/util/windows_test_util.h"
24
Loo Rong Jie4022bac2018-06-11 02:04:52 -070025#if !defined(_WIN32) && !defined(__CYGWIN__)
Laszlo Csomord2cba292017-02-10 10:38:14 +000026#error("This test should only be run on Windows")
Loo Rong Jie4022bac2018-06-11 02:04:52 -070027#endif // !defined(_WIN32) && !defined(__CYGWIN__)
Laszlo Csomord2cba292017-02-10 10:38:14 +000028
29namespace blaze_util {
30
31using std::wstring;
32
33class WindowsTestUtilTest : public ::testing::Test {
34 public:
35 void SetUp() override { ::CreateDirectoryW(GetTestTmpDirW().c_str(), NULL); }
36 void TearDown() override { DeleteAllUnder(GetTestTmpDirW()); }
37};
38
39TEST_F(WindowsTestUtilTest, TestGetTestTempDirW) {
40 wstring actual = GetTestTmpDirW();
41 ASSERT_EQ(actual.find(L":\\"), 1);
42 ASSERT_EQ(actual.find(L"/"), wstring::npos);
43}
44
45TEST_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
54TEST_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