summaryrefslogtreecommitdiff
path: root/test/config_test.cc
blob: d78b9357b86dc8943fcb5021255392c813fa09ed (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
/*
    Copyright (C) 2018-2021 Carl Hetherington <cth@carlh.net>

    This file is part of DCP-o-matic.

    DCP-o-matic is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 2 of the License, or
    (at your option) any later version.

    DCP-o-matic is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.

*/


#include "lib/config.h"
#include "test.h"
#include <boost/test/unit_test.hpp>
#include <fstream>


using std::ofstream;


static void
rewrite_bad_config ()
{
	boost::system::error_code ec;
	boost::filesystem::remove ("build/test/bad_config/config.xml", ec);

	Config::override_path = "build/test/bad_config";
	boost::filesystem::create_directories ("build/test/bad_config");
	ofstream f ("build/test/bad_config/config.xml");
	f << "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
	  << "<Config>\n"
	  << "<Foo></Foo>\n"
	  << "</Config>\n";
	f.close ();
}


BOOST_AUTO_TEST_CASE (config_backup_test)
{
	Config::override_path = "build/test/bad_config";

	Config::drop();

	boost::filesystem::remove_all ("build/test/bad_config");

	rewrite_bad_config();

	Config::instance();

	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.1"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.2"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.3"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.4"));

	Config::drop();
	rewrite_bad_config();
	Config::instance();

	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.1"));
	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.2"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.3"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.4"));

	Config::drop();
	rewrite_bad_config();
	Config::instance();

	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.1"));
	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.2"));
	BOOST_CHECK ( boost::filesystem::exists ("build/test/bad_config/config.xml.3"));
	BOOST_CHECK (!boost::filesystem::exists ("build/test/bad_config/config.xml.4"));

	Config::drop();
	rewrite_bad_config();
	Config::instance();

	BOOST_CHECK (boost::filesystem::exists ("build/test/bad_config/config.xml.1"));
	BOOST_CHECK (boost::filesystem::exists ("build/test/bad_config/config.xml.2"));
	BOOST_CHECK (boost::filesystem::exists ("build/test/bad_config/config.xml.3"));
	BOOST_CHECK (boost::filesystem::exists ("build/test/bad_config/config.xml.4"));

	/* This test has called Config::set_defaults(), so take us back
	   to the config that we want for our tests.
	*/
	setup_test_config ();
}


BOOST_AUTO_TEST_CASE (config_write_utf8_test)
{
	boost::filesystem::remove_all ("build/test/config.xml");
	boost::filesystem::copy_file ("test/data/utf8_config.xml", "build/test/config.xml");
	Config::override_path = "build/test";
	Config::drop ();
	Config::instance()->write();

	check_text_file ("test/data/utf8_config.xml", "build/test/config.xml");

	/* This test has called Config::set_defaults(), so take us back
	   to the config that we want for our tests.
	*/
	setup_test_config ();
}