blob: 391c0baaadf5c6d7e56b210800cd880f2df746ff [file] [log] [blame]
Michal Gorny593970f2016-11-28 21:11:22 +00001//===- unittests/Driver/DistroTest.cpp --- ToolChains tests ---------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Michal Gorny593970f2016-11-28 21:11:22 +00006//
7//===----------------------------------------------------------------------===//
8//
9// Unit tests for Distro detection.
10//
11//===----------------------------------------------------------------------===//
12
13#include "clang/Driver/Distro.h"
Jonas Devliegherefc514902018-10-10 13:27:25 +000014#include "llvm/Support/VirtualFileSystem.h"
Michal Gorny593970f2016-11-28 21:11:22 +000015#include "llvm/Support/raw_ostream.h"
16#include "gtest/gtest.h"
17using namespace clang;
18using namespace clang::driver;
19
20namespace {
21
22// The tests include all release-related files for each distribution
23// in the VFS, in order to make sure that earlier tests do not
24// accidentally result in incorrect distribution guess.
25
26TEST(DistroTest, DetectUbuntu) {
Jonas Devliegherefc514902018-10-10 13:27:25 +000027 llvm::vfs::InMemoryFileSystem UbuntuTrustyFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +000028 // Ubuntu uses Debian Sid version.
29 UbuntuTrustyFileSystem.addFile("/etc/debian_version", 0,
30 llvm::MemoryBuffer::getMemBuffer("jessie/sid\n"));
31 UbuntuTrustyFileSystem.addFile("/etc/lsb-release", 0,
32 llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
33 "DISTRIB_RELEASE=14.04\n"
34 "DISTRIB_CODENAME=trusty\n"
35 "DISTRIB_DESCRIPTION=\"Ubuntu 14.04 LTS\"\n"));
36 UbuntuTrustyFileSystem.addFile("/etc/os-release", 0,
37 llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
38 "VERSION=\"14.04, Trusty Tahr\"\n"
39 "ID=ubuntu\n"
40 "ID_LIKE=debian\n"
41 "PRETTY_NAME=\"Ubuntu 14.04 LTS\"\n"
42 "VERSION_ID=\"14.04\"\n"
43 "HOME_URL=\"http://www.ubuntu.com/\"\n"
44 "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"
45 "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"));
46
Alexandre Ganea1abd4c92019-11-28 15:56:00 -050047 Distro UbuntuTrusty{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +000048 ASSERT_EQ(Distro(Distro::UbuntuTrusty), UbuntuTrusty);
49 ASSERT_TRUE(UbuntuTrusty.IsUbuntu());
50 ASSERT_FALSE(UbuntuTrusty.IsRedhat());
51 ASSERT_FALSE(UbuntuTrusty.IsOpenSUSE());
52 ASSERT_FALSE(UbuntuTrusty.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +000053 ASSERT_FALSE(UbuntuTrusty.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +000054
Alexandre Ganea1abd4c92019-11-28 15:56:00 -050055 Distro UbuntuTrusty2{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-windows")};
56 ASSERT_EQ(Distro(Distro::UnknownDistro), UbuntuTrusty2);
57
Jonas Devliegherefc514902018-10-10 13:27:25 +000058 llvm::vfs::InMemoryFileSystem UbuntuYakketyFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +000059 UbuntuYakketyFileSystem.addFile("/etc/debian_version", 0,
60 llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
61 UbuntuYakketyFileSystem.addFile("/etc/lsb-release", 0,
62 llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
63 "DISTRIB_RELEASE=16.10\n"
64 "DISTRIB_CODENAME=yakkety\n"
65 "DISTRIB_DESCRIPTION=\"Ubuntu 16.10\"\n"));
66 UbuntuYakketyFileSystem.addFile("/etc/os-release", 0,
67 llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
68 "VERSION=\"16.10 (Yakkety Yak)\"\n"
69 "ID=ubuntu\n"
70 "ID_LIKE=debian\n"
71 "PRETTY_NAME=\"Ubuntu 16.10\"\n"
72 "VERSION_ID=\"16.10\"\n"
73 "HOME_URL=\"http://www.ubuntu.com/\"\n"
74 "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"
75 "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"
76 "PRIVACY_POLICY_URL=\"http://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n"
77 "VERSION_CODENAME=yakkety\n"
78 "UBUNTU_CODENAME=yakkety\n"));
79
Alexandre Ganea1abd4c92019-11-28 15:56:00 -050080 Distro UbuntuYakkety{UbuntuYakketyFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +000081 ASSERT_EQ(Distro(Distro::UbuntuYakkety), UbuntuYakkety);
82 ASSERT_TRUE(UbuntuYakkety.IsUbuntu());
83 ASSERT_FALSE(UbuntuYakkety.IsRedhat());
84 ASSERT_FALSE(UbuntuYakkety.IsOpenSUSE());
85 ASSERT_FALSE(UbuntuYakkety.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +000086 ASSERT_FALSE(UbuntuYakkety.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +000087}
88
89TEST(DistroTest, DetectRedhat) {
Jonas Devliegherefc514902018-10-10 13:27:25 +000090 llvm::vfs::InMemoryFileSystem Fedora25FileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +000091 Fedora25FileSystem.addFile("/etc/system-release-cpe", 0,
92 llvm::MemoryBuffer::getMemBuffer("cpe:/o:fedoraproject:fedora:25\n"));
93 // Both files are symlinks to fedora-release.
94 Fedora25FileSystem.addFile("/etc/system-release", 0,
95 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
96 Fedora25FileSystem.addFile("/etc/redhat-release", 0,
97 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
98 Fedora25FileSystem.addFile("/etc/fedora-release", 0,
99 llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
100 Fedora25FileSystem.addFile("/etc/os-release", 0,
101 llvm::MemoryBuffer::getMemBuffer("NAME=Fedora\n"
102 "VERSION=\"25 (Twenty Five)\"\n"
103 "ID=fedora\n"
104 "VERSION_ID=25\n"
105 "PRETTY_NAME=\"Fedora 25 (Twenty Five)\"\n"
106 "ANSI_COLOR=\"0;34\"\n"
107 "CPE_NAME=\"cpe:/o:fedoraproject:fedora:25\"\n"
108 "HOME_URL=\"https://fedoraproject.org/\"\n"
109 "BUG_REPORT_URL=\"https://bugzilla.redhat.com/\"\n"
110 "REDHAT_BUGZILLA_PRODUCT=\"Fedora\"\n"
111 "REDHAT_BUGZILLA_PRODUCT_VERSION=25\n"
112 "REDHAT_SUPPORT_PRODUCT=\"Fedora\"\n"
113 "REDHAT_SUPPORT_PRODUCT_VERSION=25\n"
114 "PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy\n"));
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500115 Distro Fedora25{Fedora25FileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000116 ASSERT_EQ(Distro(Distro::Fedora), Fedora25);
117 ASSERT_FALSE(Fedora25.IsUbuntu());
118 ASSERT_TRUE(Fedora25.IsRedhat());
119 ASSERT_FALSE(Fedora25.IsOpenSUSE());
120 ASSERT_FALSE(Fedora25.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000121 ASSERT_FALSE(Fedora25.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000122
Jonas Devliegherefc514902018-10-10 13:27:25 +0000123 llvm::vfs::InMemoryFileSystem CentOS7FileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000124 CentOS7FileSystem.addFile("/etc/system-release-cpe", 0,
125 llvm::MemoryBuffer::getMemBuffer("cpe:/o:centos:centos:7\n"));
126 // Both files are symlinks to centos-release.
127 CentOS7FileSystem.addFile("/etc/system-release", 0,
128 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
129 CentOS7FileSystem.addFile("/etc/redhat-release", 0,
130 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
131 CentOS7FileSystem.addFile("/etc/centos-release", 0,
132 llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
133 CentOS7FileSystem.addFile("/etc/centos-release-upstream", 0,
134 llvm::MemoryBuffer::getMemBuffer("Derived from Red Hat Enterprise Linux 7.2 (Source)\n"));
135 CentOS7FileSystem.addFile("/etc/os-release", 0,
136 llvm::MemoryBuffer::getMemBuffer("NAME=\"CentOS Linux\"\n"
137 "VERSION=\"7 (Core)\"\n"
138 "ID=\"centos\"\n"
139 "ID_LIKE=\"rhel fedora\"\n"
140 "VERSION_ID=\"7\"\n"
141 "PRETTY_NAME=\"CentOS Linux 7 (Core)\"\n"
142 "ANSI_COLOR=\"0;31\"\n"
143 "CPE_NAME=\"cpe:/o:centos:centos:7\"\n"
144 "HOME_URL=\"https://www.centos.org/\"\n"
145 "BUG_REPORT_URL=\"https://bugs.centos.org/\"\n"
146 "\n"
147 "CENTOS_MANTISBT_PROJECT=\"CentOS-7\"\n"
148 "CENTOS_MANTISBT_PROJECT_VERSION=\"7\"\n"
149 "REDHAT_SUPPORT_PRODUCT=\"centos\"\n"
150 "REDHAT_SUPPORT_PRODUCT_VERSION=\"7\"\n"));
151
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500152 Distro CentOS7{CentOS7FileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000153 ASSERT_EQ(Distro(Distro::RHEL7), CentOS7);
154 ASSERT_FALSE(CentOS7.IsUbuntu());
155 ASSERT_TRUE(CentOS7.IsRedhat());
156 ASSERT_FALSE(CentOS7.IsOpenSUSE());
157 ASSERT_FALSE(CentOS7.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000158 ASSERT_FALSE(CentOS7.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000159}
160
161TEST(DistroTest, DetectOpenSUSE) {
Jonas Devliegherefc514902018-10-10 13:27:25 +0000162 llvm::vfs::InMemoryFileSystem OpenSUSELeap421FileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000163 OpenSUSELeap421FileSystem.addFile("/etc/SuSE-release", 0,
164 llvm::MemoryBuffer::getMemBuffer("openSUSE 42.1 (x86_64)\n"
165 "VERSION = 42.1\n"
166 "CODENAME = Malachite\n"
167 "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));
168 OpenSUSELeap421FileSystem.addFile("/etc/os-release", 0,
169 llvm::MemoryBuffer::getMemBuffer("NAME=\"openSUSE Leap\"\n"
170 "VERSION=\"42.1\"\n"
171 "VERSION_ID=\"42.1\"\n"
172 "PRETTY_NAME=\"openSUSE Leap 42.1 (x86_64)\"\n"
173 "ID=opensuse\n"
174 "ANSI_COLOR=\"0;32\"\n"
175 "CPE_NAME=\"cpe:/o:opensuse:opensuse:42.1\"\n"
176 "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"
177 "HOME_URL=\"https://opensuse.org/\"\n"
178 "ID_LIKE=\"suse\"\n"));
179
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500180 Distro OpenSUSELeap421{OpenSUSELeap421FileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000181 ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSELeap421);
182 ASSERT_FALSE(OpenSUSELeap421.IsUbuntu());
183 ASSERT_FALSE(OpenSUSELeap421.IsRedhat());
184 ASSERT_TRUE(OpenSUSELeap421.IsOpenSUSE());
185 ASSERT_FALSE(OpenSUSELeap421.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000186 ASSERT_FALSE(OpenSUSELeap421.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000187
Jonas Devliegherefc514902018-10-10 13:27:25 +0000188 llvm::vfs::InMemoryFileSystem OpenSUSE132FileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000189 OpenSUSE132FileSystem.addFile("/etc/SuSE-release", 0,
190 llvm::MemoryBuffer::getMemBuffer("openSUSE 13.2 (x86_64)\n"
191 "VERSION = 13.2\n"
192 "CODENAME = Harlequin\n"
193 "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));
194 OpenSUSE132FileSystem.addFile("/etc/os-release", 0,
195 llvm::MemoryBuffer::getMemBuffer("NAME=openSUSE\n"
196 "VERSION=\"13.2 (Harlequin)\"\n"
197 "VERSION_ID=\"13.2\"\n"
198 "PRETTY_NAME=\"openSUSE 13.2 (Harlequin) (x86_64)\"\n"
199 "ID=opensuse\n"
200 "ANSI_COLOR=\"0;32\"\n"
201 "CPE_NAME=\"cpe:/o:opensuse:opensuse:13.2\"\n"
202 "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"
203 "HOME_URL=\"https://opensuse.org/\"\n"
204 "ID_LIKE=\"suse\"\n"));
205
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500206 Distro OpenSUSE132{OpenSUSE132FileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000207 ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSE132);
208 ASSERT_FALSE(OpenSUSE132.IsUbuntu());
209 ASSERT_FALSE(OpenSUSE132.IsRedhat());
210 ASSERT_TRUE(OpenSUSE132.IsOpenSUSE());
211 ASSERT_FALSE(OpenSUSE132.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000212 ASSERT_FALSE(OpenSUSE132.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000213
Jonas Devliegherefc514902018-10-10 13:27:25 +0000214 llvm::vfs::InMemoryFileSystem SLES10FileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000215 SLES10FileSystem.addFile("/etc/SuSE-release", 0,
216 llvm::MemoryBuffer::getMemBuffer("SUSE Linux Enterprise Server 10 (x86_64)\n"
217 "VERSION = 10\n"
218 "PATCHLEVEL = 4\n"));
219 SLES10FileSystem.addFile("/etc/lsb_release", 0,
220 llvm::MemoryBuffer::getMemBuffer("LSB_VERSION=\"core-2.0-noarch:core-3.0-noarch:core-2.0-x86_64:core-3.0-x86_64\"\n"));
221
222 // SLES10 is unsupported and therefore evaluates to unknown
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500223 Distro SLES10{SLES10FileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000224 ASSERT_EQ(Distro(Distro::UnknownDistro), SLES10);
225 ASSERT_FALSE(SLES10.IsUbuntu());
226 ASSERT_FALSE(SLES10.IsRedhat());
227 ASSERT_FALSE(SLES10.IsOpenSUSE());
228 ASSERT_FALSE(SLES10.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000229 ASSERT_FALSE(SLES10.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000230}
231
232TEST(DistroTest, DetectDebian) {
Jonas Devliegherefc514902018-10-10 13:27:25 +0000233 llvm::vfs::InMemoryFileSystem DebianJessieFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000234 DebianJessieFileSystem.addFile("/etc/debian_version", 0,
235 llvm::MemoryBuffer::getMemBuffer("8.6\n"));
236 DebianJessieFileSystem.addFile("/etc/os-release", 0,
237 llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n"
238 "NAME=\"Debian GNU/Linux\"\n"
239 "VERSION_ID=\"8\"\n"
240 "VERSION=\"8 (jessie)\"\n"
241 "ID=debian\n"
242 "HOME_URL=\"http://www.debian.org/\"\n"
243 "SUPPORT_URL=\"http://www.debian.org/support\"\n"
244 "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));
245
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500246 Distro DebianJessie{DebianJessieFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000247 ASSERT_EQ(Distro(Distro::DebianJessie), DebianJessie);
248 ASSERT_FALSE(DebianJessie.IsUbuntu());
249 ASSERT_FALSE(DebianJessie.IsRedhat());
250 ASSERT_FALSE(DebianJessie.IsOpenSUSE());
251 ASSERT_TRUE(DebianJessie.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000252 ASSERT_FALSE(DebianJessie.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000253
Jonas Devliegherefc514902018-10-10 13:27:25 +0000254 llvm::vfs::InMemoryFileSystem DebianStretchSidFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000255 DebianStretchSidFileSystem.addFile("/etc/debian_version", 0,
256 llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
257 DebianStretchSidFileSystem.addFile("/etc/os-release", 0,
258 llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux stretch/sid\"\n"
259 "NAME=\"Debian GNU/Linux\"\n"
260 "ID=debian\n"
261 "HOME_URL=\"http://www.debian.org/\"\n"
262 "SUPPORT_URL=\"http://www.debian.org/support\"\n"
263 "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));
264
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500265 Distro DebianStretchSid{DebianStretchSidFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000266 ASSERT_EQ(Distro(Distro::DebianStretch), DebianStretchSid);
267 ASSERT_FALSE(DebianStretchSid.IsUbuntu());
268 ASSERT_FALSE(DebianStretchSid.IsRedhat());
269 ASSERT_FALSE(DebianStretchSid.IsOpenSUSE());
270 ASSERT_TRUE(DebianStretchSid.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000271 ASSERT_FALSE(DebianStretchSid.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000272}
273
274TEST(DistroTest, DetectExherbo) {
Jonas Devliegherefc514902018-10-10 13:27:25 +0000275 llvm::vfs::InMemoryFileSystem ExherboFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000276 ExherboFileSystem.addFile("/etc/exherbo-release", 0, // (ASCII art)
277 llvm::MemoryBuffer::getMemBuffer(""));
278 ExherboFileSystem.addFile("/etc/os-release", 0,
279 llvm::MemoryBuffer::getMemBuffer("NAME=\"Exherbo\"\n"
280 "PRETTY_NAME=\"Exherbo Linux\"\n"
281 "ID=\"exherbo\"\n"
282 "ANSI_COLOR=\"0;32\"\n"
283 "HOME_URL=\"https://www.exherbo.org/\"\n"
284 "SUPPORT_URL=\"irc://irc.freenode.net/#exherbo\"\n"
285 "BUG_REPORT_URL=\"https://bugs.exherbo.org/\"\n"));
286
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500287 Distro Exherbo{ExherboFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000288 ASSERT_EQ(Distro(Distro::Exherbo), Exherbo);
289 ASSERT_FALSE(Exherbo.IsUbuntu());
290 ASSERT_FALSE(Exherbo.IsRedhat());
291 ASSERT_FALSE(Exherbo.IsOpenSUSE());
292 ASSERT_FALSE(Exherbo.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000293 ASSERT_FALSE(Exherbo.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000294}
295
296TEST(DistroTest, DetectArchLinux) {
Jonas Devliegherefc514902018-10-10 13:27:25 +0000297 llvm::vfs::InMemoryFileSystem ArchLinuxFileSystem;
Michal Gorny593970f2016-11-28 21:11:22 +0000298 ArchLinuxFileSystem.addFile("/etc/arch-release", 0, // (empty)
299 llvm::MemoryBuffer::getMemBuffer(""));
300 ArchLinuxFileSystem.addFile("/etc/os-release", 0,
301 llvm::MemoryBuffer::getMemBuffer("NAME=\"Arch Linux\"\n"
302 "ID=arch\n"
303 "PRETTY_NAME=\"Arch Linux\"\n"
304 "ANSI_COLOR=\"0;36\"\n"
305 "HOME_URL=\"https://www.archlinux.org/\"\n"
306 "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
307 "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"));
308
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500309 Distro ArchLinux{ArchLinuxFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gorny593970f2016-11-28 21:11:22 +0000310 ASSERT_EQ(Distro(Distro::ArchLinux), ArchLinux);
311 ASSERT_FALSE(ArchLinux.IsUbuntu());
312 ASSERT_FALSE(ArchLinux.IsRedhat());
313 ASSERT_FALSE(ArchLinux.IsOpenSUSE());
314 ASSERT_FALSE(ArchLinux.IsDebian());
Michal Gornyf2412282018-12-23 15:07:19 +0000315 ASSERT_FALSE(ArchLinux.IsGentoo());
316}
317
318TEST(DistroTest, DetectGentoo) {
319 llvm::vfs::InMemoryFileSystem GentooFileSystem;
320 GentooFileSystem.addFile(
321 "/etc/gentoo-release", 0,
322 llvm::MemoryBuffer::getMemBuffer("Gentoo Base System release 2.6"));
323 GentooFileSystem.addFile(
324 "/etc/os-release", 0,
325 llvm::MemoryBuffer::getMemBuffer(
326 "NAME=Gentoo\n"
327 "ID=gentoo\n"
328 "PRETTY_NAME=\"Gentoo/Linux\"\n"
329 "ANSI_COLOR=\"1;32\"\n"
330 "HOME_URL=\"https://www.gentoo.org/\"\n"
331 "SUPPORT_URL=\"https://www.gentoo.org/support/\"\n"
332 "BUG_REPORT_URL=\"https://bugs.gentoo.org/\"\n"));
333
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500334 Distro Gentoo{GentooFileSystem, llvm::Triple("unknown-pc-linux")};
Michal Gornyf2412282018-12-23 15:07:19 +0000335 ASSERT_EQ(Distro(Distro::Gentoo), Gentoo);
336 ASSERT_FALSE(Gentoo.IsUbuntu());
337 ASSERT_FALSE(Gentoo.IsRedhat());
338 ASSERT_FALSE(Gentoo.IsOpenSUSE());
339 ASSERT_FALSE(Gentoo.IsDebian());
340 ASSERT_TRUE(Gentoo.IsGentoo());
Michal Gorny593970f2016-11-28 21:11:22 +0000341}
342
Alexandre Ganea1abd4c92019-11-28 15:56:00 -0500343TEST(DistroTest, DetectWindowsAndCrossCompile) {
344
345 class CountingFileSystem : public llvm::vfs::ProxyFileSystem {
346 public:
347 CountingFileSystem() : ProxyFileSystem(llvm::vfs::getRealFileSystem()) {}
348
349 llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override {
350 ++Count;
351 return llvm::vfs::ProxyFileSystem::status(Path);
352 }
353
354 llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
355 openFileForRead(const llvm::Twine &Path) override {
356 ++Count;
357 return llvm::vfs::ProxyFileSystem::openFileForRead(Path);
358 }
359
360 unsigned Count{};
361 };
362
363 llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RFS =
364 llvm::vfs::getRealFileSystem();
365 llvm::Triple Host(llvm::sys::getProcessTriple());
366
367 CountingFileSystem CFileSystem;
368 Distro LinuxDistro{CFileSystem, llvm::Triple("unknown-pc-linux")};
369 if (Host.isOSWindows()) {
370 ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxDistro);
371 ASSERT_GT(CFileSystem.Count, 0U);
372 }
373
374 Distro WinDistro{CFileSystem, llvm::Triple("unknown-pc-windows")};
375 ASSERT_EQ(Distro(Distro::UnknownDistro), WinDistro);
376 ASSERT_GT(CFileSystem.Count, 0U);
377
378 // When running on Windows along with a real file system, ensure that no
379 // distro is returned if targeting Linux
380 if (Host.isOSWindows()) {
381 Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};
382 ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxRealDistro);
383 }
384 // When running on Linux, check if the distro is the same as the host when
385 // targeting Linux
386 if (Host.isOSLinux()) {
387 Distro HostDistro{*RFS, Host};
388 Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};
389 ASSERT_EQ(HostDistro, LinuxRealDistro);
390 }
391
392 Distro WinRealDistro{*RFS, llvm::Triple("unknown-pc-windows")};
393 ASSERT_EQ(Distro(Distro::UnknownDistro), WinRealDistro);
394}
395
Michal Gorny593970f2016-11-28 21:11:22 +0000396} // end anonymous namespace