The Battle for Wesnoth  1.13.4+dev
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
test_filesystem.cpp
Go to the documentation of this file.
1 /*
2  Copyright (C) 2015 - 2016 by Ignacio R. Morelle <[email protected]>
3  Part of the Battle for Wesnoth Project http://www.wesnoth.org/
4 
5  This program is free software; you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation; either version 2 of the License, or
8  (at your option) any later version.
9  This program is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY.
11 
12  See the COPYING file for more details.
13 */
14 
15 #include <boost/test/unit_test.hpp>
16 #include "filesystem.hpp"
17 #include "game_config.hpp"
18 
19 #if 0
20 namespace {
21 
22 template<typename T>
23 void dump(const T& v)
24 {
25  for(typename T::const_iterator k = v.begin(); k != v.end(); ++k) {
26  std::cerr << " * " << *k << '\n';
27  }
28 }
29 
30 }
31 #endif
32 
33 BOOST_AUTO_TEST_SUITE( filesystem ) // implicit namespace filesystem
34 
35 const std::string& gamedata = game_config::path;
36 
37 BOOST_AUTO_TEST_CASE( test_fs_game_path_reverse_engineering )
38 {
39  const std::string maincfg = "_main.cfg";
40 
41  std::string gamedata_rev = get_wml_location("_main.cfg");
42 
43  const size_t strip_len = (maincfg + "/data/").length();
44  BOOST_REQUIRE(gamedata_rev.length() > strip_len);
45  gamedata_rev.resize(gamedata_rev.length() - strip_len);
46 
47  BOOST_CHECK_EQUAL( gamedata_rev, gamedata );
48 }
49 
50 BOOST_AUTO_TEST_CASE( test_fs_base )
51 {
52  BOOST_CHECK( is_directory("/") );
53  BOOST_CHECK( is_directory("/.") );
54  BOOST_CHECK( is_directory("/./././.") );
55  BOOST_CHECK( is_directory("/..") );
56 
57  BOOST_CHECK( is_directory(".") );
58  BOOST_CHECK( is_directory("..") );
59  BOOST_CHECK( is_directory("./././.") );
60 
61  BOOST_CHECK( is_directory(gamedata + "/data/core/../../data/././../data/././core") );
62 
63  BOOST_CHECK( file_exists("/") );
64  BOOST_CHECK( file_exists("/.") );
65  BOOST_CHECK( file_exists("/./././.") );
66  BOOST_CHECK( file_exists("/..") );
67 
68  BOOST_CHECK_EQUAL( base_name("foo/bar/baz.cfg"), "baz.cfg" );
69  // FIXME: BFS gives "." for this, Unix basename gives "bar"!
70  //BOOST_CHECK_EQUAL( base_name("foo/bar/"), "bar" );
71  BOOST_CHECK_EQUAL( base_name("foo/bar"), "bar" );
72  BOOST_CHECK_EQUAL( base_name("/"), "/" );
73  BOOST_CHECK_EQUAL( base_name(""), "" );
74 
75  BOOST_CHECK_EQUAL( directory_name("foo/bar/baz.cfg"), "foo/bar" );
76  BOOST_CHECK_EQUAL( directory_name("foo/bar/"), "foo/bar" );
77  BOOST_CHECK_EQUAL( directory_name("foo/bar"), "foo" );
78  BOOST_CHECK_EQUAL( directory_name("/"), "" );
79  BOOST_CHECK_EQUAL( directory_name(""), "" );
80 
81  // TODO normalize_path
82 
83  //BOOST_CHECK_EQUAL( normalize_path(gamedata + "/data/core/../../data/././../data/././core"),
84  // gamedata + "/data/core" );
85 }
86 
87 BOOST_AUTO_TEST_CASE( test_fs_enum )
88 {
89  const std::string path = "data/test/test/filesystem/enum";
90 
91  const std::vector<std::string> expected_filenames = {
92  "_initial.cfg",
93  "A1.cfg",
94  "A2.cfg",
95  "A3.cfg",
96  "B1.cfg",
97  "B2.cfg",
98  "B3.cfg",
99  "_final.cfg"};
100  const std::vector<std::string> expected_dirnames = {
101  "D1",
102  "D2",
103  "D3"};
104 
105  std::vector<std::string> files, dirs;
106  std::vector<std::string> expected_filepaths, expected_dirpaths;
107 
108  for(const std::string& n : expected_filenames) {
109  expected_filepaths.push_back(gamedata + "/" + path + "/" + n);
110  }
111 
112  for(const std::string& n : expected_dirnames) {
113  expected_dirpaths.push_back(gamedata + "/" + path + "/" + n);
114  }
115 
116  // FIXME: get_files_in_dir with mode == FILE_NAME_ONLY will fail to reorder
117  // entries because the sorting code looks for forward slashes.
118  // This affects both the BFS-based and legacy implementations.
119  get_files_in_dir(path, &files, &dirs, ENTIRE_FILE_PATH, NO_FILTER, DO_REORDER);
120 
121  BOOST_CHECK( files == expected_filepaths );
122  BOOST_CHECK( dirs == expected_dirpaths );
123 }
124 
125 BOOST_AUTO_TEST_CASE( test_fs_binary_path )
126 {
127  BOOST_CHECK_EQUAL( get_binary_dir_location("images", "."), gamedata + "/images/." );
128 
129  BOOST_CHECK_EQUAL( get_binary_file_location("images", "././././././"),
130  gamedata + "/images/././././././" );
131 
132  BOOST_CHECK_EQUAL( get_binary_file_location("images", "wesnoth-icon.png"),
133  gamedata + "/data/core/images/wesnoth-icon.png" );
134 
135  BOOST_CHECK_EQUAL( get_binary_file_location("music", "silence.ogg"),
136  gamedata + "/data/core/music/silence.ogg" );
137 
138  BOOST_CHECK_EQUAL( get_binary_file_location("sounds", "explosion.ogg"),
139  gamedata + "/data/core/sounds/explosion.ogg" );
140 
141  BOOST_CHECK_EQUAL( get_independent_image_path("wesnoth-icon.png"),
142  "data/core/images/wesnoth-icon.png" );
143 
144  // Inexistent paths are resolved empty.
145  BOOST_CHECK( get_binary_dir_location("images", "").empty() );
146  BOOST_CHECK( get_binary_dir_location("inexistent_resource_type", "").empty() );
147  BOOST_CHECK( get_binary_file_location("image", "wesnoth-icon.png").empty() );
148  BOOST_CHECK( get_binary_file_location("images", "bunnies_and_ponies_and_rainbows_oh_em_gee.psd").empty() );
149  BOOST_CHECK( get_binary_file_location("music", "this_track_does_not_exist.aiff").empty() );
150  BOOST_CHECK( get_binary_file_location("sounds", "rude_noises.aiff").empty() );
151  BOOST_CHECK( get_independent_image_path("dopefish.txt").empty() );
152 }
153 
154 BOOST_AUTO_TEST_CASE( test_fs_wml_path )
155 {
156  const std::string& userdata = get_user_data_dir();
157 
158  BOOST_CHECK_EQUAL( get_wml_location(""), "" );
159 
160  BOOST_CHECK_EQUAL( get_wml_location("_main.cfg"), gamedata + "/data/_main.cfg" );
161  BOOST_CHECK_EQUAL( get_wml_location("core/_main.cfg"), gamedata + "/data/core/_main.cfg" );
162  BOOST_CHECK_EQUAL( get_wml_location("."), gamedata + "/data/." );
163 
164  BOOST_CHECK_EQUAL( get_wml_location("~"), userdata + "/data" );
165  BOOST_CHECK_EQUAL( get_wml_location("~/"), userdata + "/data/" );
166 
167  // Inexistent paths are resolved empty.
168  BOOST_CHECK( get_wml_location("why_would_anyone_ever_name_a_file_like_this").empty() );
169 }
170 
171 BOOST_AUTO_TEST_CASE( test_fs_fluff )
172 {
173  BOOST_CHECK( ends_with("foobarbazbat", "bazbat") );
174 
175  BOOST_CHECK( looks_like_pbl("foo.pbl") );
176  BOOST_CHECK( looks_like_pbl("FOO.PBL") );
177  BOOST_CHECK( looks_like_pbl("Foo.Pbl") );
178  BOOST_CHECK( !looks_like_pbl("foo.pbl.cfg") );
179 
180  BOOST_CHECK( is_gzip_file("foo.gz") );
181  BOOST_CHECK( !is_gzip_file("foo.gz.bz2") );
182  BOOST_CHECK( is_bzip2_file("foo.bz2") );
183  BOOST_CHECK( !is_bzip2_file("foo.bz2.gz") );
184 
185  BOOST_CHECK( is_compressed_file("foo.gz") );
186  BOOST_CHECK( is_compressed_file("foo.bz2") );
187  BOOST_CHECK( !is_compressed_file("foo.txt") );
188 
189  // FIXME: Is this even intended?
190  BOOST_CHECK( !is_gzip_file("foo.GZ") );
191  BOOST_CHECK( !is_bzip2_file("foo.BZ2") );
192  BOOST_CHECK( !is_compressed_file("foo.GZ") );
193  BOOST_CHECK( !is_compressed_file("foo.BZ2") );
194 }
195 
196 BOOST_AUTO_TEST_SUITE_END()
std::string get_binary_dir_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual directory of a given type or an empty string if the directory i...
BOOST_AUTO_TEST_SUITE(filesystem) const std BOOST_AUTO_TEST_CASE(test_fs_game_path_reverse_engineering)
bool looks_like_pbl(const std::string &file)
bool ends_with(const std::string &str, const std::string &suffix)
STL namespace.
BOOST_AUTO_TEST_SUITE(test_map_location)
std::string get_binary_file_location(const std::string &type, const std::string &filename)
Returns a complete path to the actual file of a given type or an empty string if the file isn't prese...
game_data * gamedata
Definition: resources.cpp:22
GLuint GLsizei GLsizei * length
Definition: glew.h:1793
GLsizei const char ** path
Definition: glew.h:4654
std::string get_user_data_dir()
const GLdouble * v
Definition: glew.h:1359
std::string base_name(const std::string &file)
Returns the base filename of a file, with directory name stripped.
bool is_directory(const std::string &fname)
Returns true if the given file is a directory.
std::string get_independent_image_path(const std::string &filename)
Returns an image path to filename for binary path-independent use in saved games. ...
bool is_gzip_file(const std::string &filename)
Returns true if the file ends with '.gz'.
void get_files_in_dir(const std::string &dir, std::vector< std::string > *files, std::vector< std::string > *dirs=nullptr, file_name_option mode=FILE_NAME_ONLY, file_filter_option filter=NO_FILTER, file_reorder_option reorder=DONT_REORDER, file_tree_checksum *checksum=nullptr)
Populates 'files' with all the files and 'dirs' with all the directories in dir.
std::string get_wml_location(const std::string &filename, const std::string &current_dir=std::string())
Returns a complete path to the actual WML file or directory or an empty string if the file isn't pres...
Game configuration data as global variables.
Definition: build_info.cpp:38
bool is_compressed_file(const std::string &filename)
Definition: filesystem.hpp:144
Declarations for File-IO.
bool is_bzip2_file(const std::string &filename)
Returns true if the file ends with '.bz2'.
GLclampd n
Definition: glew.h:5903
bool file_exists(const std::string &name)
Returns true if a file or directory with such name already exists.
GLsizei const GLcharARB ** string
Definition: glew.h:4503
std::string directory_name(const std::string &file)
Returns the directory name of a file, with filename stripped.