Backup more than one config on failure to load.
[dcpomatic.git] / test / config_test.cc
1 /*
2     Copyright (C) 2018 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic is free software; you can redistribute it and/or modify
7     it under the terms of the GNU General Public License as published by
8     the Free Software Foundation; either version 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic is distributed in the hope that it will be useful,
12     but WITHOUT ANY WARRANTY; without even the implied warranty of
13     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14     GNU General Public License for more details.
15
16     You should have received a copy of the GNU General Public License
17     along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21 #include "lib/config.h"
22 #include <boost/test/unit_test.hpp>
23 #include <fstream>
24
25 using std::ofstream;
26
27 static void
28 rewrite_bad_config ()
29 {
30         boost::system::error_code ec;
31         boost::filesystem::remove ("build/test/config.xml", ec);
32
33         Config::test_path = "build/test";
34         ofstream f ("build/test/config.xml");
35         f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
36           << "<Config>\n"
37           << "<Foo></Foo>\n"
38           << "</Config>\n";
39         f.close ();
40 }
41
42
43 BOOST_AUTO_TEST_CASE (config_backup_test)
44 {
45         Config::drop();
46
47         boost::system::error_code ec;
48         boost::filesystem::remove ("build/test/config.xml.1", ec);
49         boost::filesystem::remove ("build/test/config.xml.2", ec);
50         boost::filesystem::remove ("build/test/config.xml.3", ec);
51         boost::filesystem::remove ("build/test/config.xml.4", ec);
52         boost::filesystem::remove ("build/test/config.xml.5", ec);
53         boost::filesystem::remove ("build/test/config.xml.5", ec);
54
55         rewrite_bad_config();
56
57         Config::instance();
58
59         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.1"));
60         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.2"));
61         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.3"));
62         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.4"));
63
64         Config::drop();
65         rewrite_bad_config();
66         Config::instance();
67
68         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.1"));
69         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.2"));
70         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.3"));
71         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.4"));
72
73         Config::drop();
74         rewrite_bad_config();
75         Config::instance();
76
77         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.1"));
78         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.2"));
79         BOOST_CHECK ( boost::filesystem::exists ("build/test/config.xml.3"));
80         BOOST_CHECK (!boost::filesystem::exists ("build/test/config.xml.4"));
81
82         Config::drop();
83         rewrite_bad_config();
84         Config::instance();
85
86         BOOST_CHECK (boost::filesystem::exists ("build/test/config.xml.1"));
87         BOOST_CHECK (boost::filesystem::exists ("build/test/config.xml.2"));
88         BOOST_CHECK (boost::filesystem::exists ("build/test/config.xml.3"));
89         BOOST_CHECK (boost::filesystem::exists ("build/test/config.xml.4"));
90 }