summaryrefslogtreecommitdiff
path: root/test/search_test.cc
blob: 179fb0e6661c72401a58c93a9bc0bdd8e9ab237b (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
#include <uunit.h>

#include <string>
#include <fstream>

#include <search.h>
#include <libctor.h>

#include "paths.h"

class SearchTest
	: public uUnit
{
public:
	SearchTest()
	{
		uTEST(SearchTest::test_include);
		uTEST(SearchTest::test_lib);
	}

	void test_include()
	{
		using namespace std::string_literals;

		constexpr auto verb = -1;

		auto src = paths::top_srcdir / "src";
		auto tmp = std::filesystem::temp_directory_path();

		{ // system
			Settings settings{.verbose = verb}; // instantiate with default paths set
			auto result = findHeader(settings, "stdio.h", {});
			uASSERT_EQUAL("/usr/include"s, result.string());
		}

		{ // libctor.h in src folder
			Settings settings{.verbose = verb}; // instantiate with default paths set
			auto result = findHeader(settings, "libctor.h", { src.string() });
			uASSERT_EQUAL(src, result);
		}

		{ // src/libctor.h in topsrc folder (ie. path with '/')
			Settings settings{.verbose = verb}; // instantiate with default paths set
			auto result = findHeader(settings, "src/libctor.h",
			                         { paths::top_srcdir.string() });
			uASSERT_EQUAL(paths::top_srcdir, result);
		}

		{ // libctor.h in src folder over one in temp folder (system)
			Settings settings{.verbose = verb}; // instantiate with default paths set
			settings.include_paths = { tmp.string() };
			auto tmpfile = tmp / "libctor.h";
			{ // create /tmp/libctor.h file
				std::ofstream of(tmpfile.string());
				of << "/* nop */";
			}
			auto result = findHeader(settings, "libctor.h", { src.string() });
			std::filesystem::remove(tmpfile);
			uASSERT_EQUAL(src, result);
		}

		{ // libctor.h in temp folder over one in src folder (local prio)
			Settings settings{.verbose = verb}; // instantiate with default paths set
			settings.include_paths = {}; // remove system search paths
			auto tmpfile = tmp / "libctor.h";
			{ // create /tmp/libctor.h file
				std::ofstream of(tmpfile.string());
				of << "/* nop */";
			}
			auto result = findHeader(settings, "libctor.h",
			                         { tmp.string(), src.string() });
			std::filesystem::remove(tmpfile);
			uASSERT_EQUAL(tmp, result);
		}

		{ // not found
			Settings settings{.verbose = verb};
			settings.include_paths = {}; // remove system search paths
			auto result = findHeader(settings, "no_such_file.h", {});
			uASSERT(result.empty());
		}
	}

	void test_lib()
	{
		using namespace std::string_literals;

		constexpr auto verb = 2;

		auto src = paths::top_srcdir / "src";
		auto tmp = std::filesystem::temp_directory_path();

		{ // system
			Settings settings{.verbose = verb}; // instantiate with default paths set
			auto result = findLibrary(settings, "ffi", {});
			for(auto f : result)
			{
				std::cout << f << "\n";
			}
			uASSERT_EQUAL("/usr/lib/libffi.so"s, result[0].string());
		}
	}
};

// Registers the fixture into the 'registry'
static SearchTest test;