Path: blob/main/contrib/kyua/utils/fs/directory_test.cpp
48081 views
// Copyright 2015 The Kyua Authors.1// All rights reserved.2//3// Redistribution and use in source and binary forms, with or without4// modification, are permitted provided that the following conditions are5// met:6//7// * Redistributions of source code must retain the above copyright8// notice, this list of conditions and the following disclaimer.9// * Redistributions in binary form must reproduce the above copyright10// notice, this list of conditions and the following disclaimer in the11// documentation and/or other materials provided with the distribution.12// * Neither the name of Google Inc. nor the names of its contributors13// may be used to endorse or promote products derived from this software14// without specific prior written permission.15//16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS17// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT18// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR19// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT20// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,21// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT22// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,23// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY24// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT25// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE26// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.2728#include "utils/fs/directory.hpp"2930#include <sstream>3132#include <atf-c++.hpp>3334#include "utils/format/containers.ipp"35#include "utils/fs/exceptions.hpp"36#include "utils/fs/operations.hpp"37#include "utils/fs/path.hpp"3839namespace fs = utils::fs;404142ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__public_fields);43ATF_TEST_CASE_BODY(directory_entry__public_fields)44{45const fs::directory_entry entry("name");46ATF_REQUIRE_EQ("name", entry.name);47}484950ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__equality);51ATF_TEST_CASE_BODY(directory_entry__equality)52{53const fs::directory_entry entry1("name");54const fs::directory_entry entry2("other-name");5556ATF_REQUIRE( entry1 == entry1);57ATF_REQUIRE(!(entry1 != entry1));5859ATF_REQUIRE(!(entry1 == entry2));60ATF_REQUIRE( entry1 != entry2);61}626364ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__sorting);65ATF_TEST_CASE_BODY(directory_entry__sorting)66{67const fs::directory_entry entry1("name");68const fs::directory_entry entry2("other-name");6970ATF_REQUIRE(!(entry1 < entry1));71ATF_REQUIRE(!(entry2 < entry2));72ATF_REQUIRE( entry1 < entry2);73ATF_REQUIRE(!(entry2 < entry1));74}757677ATF_TEST_CASE_WITHOUT_HEAD(directory_entry__format);78ATF_TEST_CASE_BODY(directory_entry__format)79{80const fs::directory_entry entry("this is the name");81std::ostringstream output;82output << entry;83ATF_REQUIRE_EQ("directory_entry{name='this is the name'}", output.str());84}858687ATF_TEST_CASE_WITHOUT_HEAD(integration__empty);88ATF_TEST_CASE_BODY(integration__empty)89{90fs::mkdir(fs::path("empty"), 0755);9192std::set< fs::directory_entry > contents;93const fs::directory dir(fs::path("empty"));94for (fs::directory::const_iterator iter = dir.begin(); iter != dir.end();95++iter) {96contents.insert(*iter);97// While we are here, make sure both * and -> represent the same.98ATF_REQUIRE((*iter).name == iter->name);99}100101std::set< fs::directory_entry > exp_contents;102exp_contents.insert(fs::directory_entry("."));103exp_contents.insert(fs::directory_entry(".."));104105ATF_REQUIRE_EQ(exp_contents, contents);106}107108109ATF_TEST_CASE_WITHOUT_HEAD(integration__some_contents);110ATF_TEST_CASE_BODY(integration__some_contents)111{112fs::mkdir(fs::path("full"), 0755);113atf::utils::create_file("full/a file", "");114atf::utils::create_file("full/something-else", "");115atf::utils::create_file("full/.hidden", "");116fs::mkdir(fs::path("full/subdir"), 0755);117atf::utils::create_file("full/subdir/not-listed", "");118119std::set< fs::directory_entry > contents;120const fs::directory dir(fs::path("full"));121for (fs::directory::const_iterator iter = dir.begin(); iter != dir.end();122++iter) {123contents.insert(*iter);124// While we are here, make sure both * and -> represent the same.125ATF_REQUIRE((*iter).name == iter->name);126}127128std::set< fs::directory_entry > exp_contents;129exp_contents.insert(fs::directory_entry("."));130exp_contents.insert(fs::directory_entry(".."));131exp_contents.insert(fs::directory_entry(".hidden"));132exp_contents.insert(fs::directory_entry("a file"));133exp_contents.insert(fs::directory_entry("something-else"));134exp_contents.insert(fs::directory_entry("subdir"));135136ATF_REQUIRE_EQ(exp_contents, contents);137}138139140ATF_TEST_CASE_WITHOUT_HEAD(integration__open_failure);141ATF_TEST_CASE_BODY(integration__open_failure)142{143const fs::directory directory(fs::path("non-existent"));144ATF_REQUIRE_THROW_RE(fs::system_error, "opendir(.*non-existent.*) failed",145directory.begin());146}147148149ATF_TEST_CASE_WITHOUT_HEAD(integration__iterators_equality);150ATF_TEST_CASE_BODY(integration__iterators_equality)151{152const fs::directory directory(fs::path("."));153154fs::directory::const_iterator iter_ok1 = directory.begin();155fs::directory::const_iterator iter_ok2 = directory.begin();156fs::directory::const_iterator iter_end = directory.end();157158ATF_REQUIRE( iter_ok1 == iter_ok1);159ATF_REQUIRE(!(iter_ok1 != iter_ok1));160161ATF_REQUIRE( iter_ok2 == iter_ok2);162ATF_REQUIRE(!(iter_ok2 != iter_ok2));163164ATF_REQUIRE(!(iter_ok1 == iter_ok2));165ATF_REQUIRE( iter_ok1 != iter_ok2);166167ATF_REQUIRE(!(iter_ok1 == iter_end));168ATF_REQUIRE( iter_ok1 != iter_end);169170ATF_REQUIRE(!(iter_ok2 == iter_end));171ATF_REQUIRE( iter_ok2 != iter_end);172173ATF_REQUIRE( iter_end == iter_end);174ATF_REQUIRE(!(iter_end != iter_end));175}176177178ATF_INIT_TEST_CASES(tcs)179{180ATF_ADD_TEST_CASE(tcs, directory_entry__public_fields);181ATF_ADD_TEST_CASE(tcs, directory_entry__equality);182ATF_ADD_TEST_CASE(tcs, directory_entry__sorting);183ATF_ADD_TEST_CASE(tcs, directory_entry__format);184185ATF_ADD_TEST_CASE(tcs, integration__empty);186ATF_ADD_TEST_CASE(tcs, integration__some_contents);187ATF_ADD_TEST_CASE(tcs, integration__open_failure);188ATF_ADD_TEST_CASE(tcs, integration__iterators_equality);189}190191192