xref: /llvm-project/clang/unittests/Driver/DistroTest.cpp (revision d768bf994f508d7eaf9541a568be3d71096febf5)
1593970f1SMichal Gorny //===- unittests/Driver/DistroTest.cpp --- ToolChains tests ---------------===//
2593970f1SMichal Gorny //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6593970f1SMichal Gorny //
7593970f1SMichal Gorny //===----------------------------------------------------------------------===//
8593970f1SMichal Gorny //
9593970f1SMichal Gorny // Unit tests for Distro detection.
10593970f1SMichal Gorny //
11593970f1SMichal Gorny //===----------------------------------------------------------------------===//
12593970f1SMichal Gorny 
13593970f1SMichal Gorny #include "clang/Driver/Distro.h"
14fc51490bSJonas Devlieghere #include "llvm/Support/VirtualFileSystem.h"
15593970f1SMichal Gorny #include "llvm/Support/raw_ostream.h"
16*d768bf99SArchibald Elliott #include "llvm/TargetParser/Host.h"
17593970f1SMichal Gorny #include "gtest/gtest.h"
18213aea4cSReid Kleckner 
19593970f1SMichal Gorny using namespace clang;
20593970f1SMichal Gorny using namespace clang::driver;
21593970f1SMichal Gorny 
22593970f1SMichal Gorny namespace {
23593970f1SMichal Gorny 
24593970f1SMichal Gorny // The tests include all release-related files for each distribution
25593970f1SMichal Gorny // in the VFS, in order to make sure that earlier tests do not
26593970f1SMichal Gorny // accidentally result in incorrect distribution guess.
27593970f1SMichal Gorny 
TEST(DistroTest,DetectUbuntu)28593970f1SMichal Gorny TEST(DistroTest, DetectUbuntu) {
29fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem UbuntuTrustyFileSystem;
30593970f1SMichal Gorny   // Ubuntu uses Debian Sid version.
31593970f1SMichal Gorny   UbuntuTrustyFileSystem.addFile("/etc/debian_version", 0,
32593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("jessie/sid\n"));
33593970f1SMichal Gorny   UbuntuTrustyFileSystem.addFile("/etc/lsb-release", 0,
34593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
35593970f1SMichal Gorny                                        "DISTRIB_RELEASE=14.04\n"
36593970f1SMichal Gorny                                        "DISTRIB_CODENAME=trusty\n"
37593970f1SMichal Gorny                                        "DISTRIB_DESCRIPTION=\"Ubuntu 14.04 LTS\"\n"));
38593970f1SMichal Gorny   UbuntuTrustyFileSystem.addFile("/etc/os-release", 0,
39593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
40593970f1SMichal Gorny                                        "VERSION=\"14.04, Trusty Tahr\"\n"
41593970f1SMichal Gorny                                        "ID=ubuntu\n"
42593970f1SMichal Gorny                                        "ID_LIKE=debian\n"
43593970f1SMichal Gorny                                        "PRETTY_NAME=\"Ubuntu 14.04 LTS\"\n"
44593970f1SMichal Gorny                                        "VERSION_ID=\"14.04\"\n"
45593970f1SMichal Gorny                                        "HOME_URL=\"http://www.ubuntu.com/\"\n"
46593970f1SMichal Gorny                                        "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"
47593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"));
48593970f1SMichal Gorny 
491abd4c94SAlexandre Ganea   Distro UbuntuTrusty{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-linux")};
50593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::UbuntuTrusty), UbuntuTrusty);
51593970f1SMichal Gorny   ASSERT_TRUE(UbuntuTrusty.IsUbuntu());
52593970f1SMichal Gorny   ASSERT_FALSE(UbuntuTrusty.IsRedhat());
53593970f1SMichal Gorny   ASSERT_FALSE(UbuntuTrusty.IsOpenSUSE());
54593970f1SMichal Gorny   ASSERT_FALSE(UbuntuTrusty.IsDebian());
55f241228aSMichal Gorny   ASSERT_FALSE(UbuntuTrusty.IsGentoo());
56593970f1SMichal Gorny 
571abd4c94SAlexandre Ganea   Distro UbuntuTrusty2{UbuntuTrustyFileSystem, llvm::Triple("unknown-pc-windows")};
581abd4c94SAlexandre Ganea   ASSERT_EQ(Distro(Distro::UnknownDistro), UbuntuTrusty2);
591abd4c94SAlexandre Ganea 
60fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem UbuntuYakketyFileSystem;
61593970f1SMichal Gorny   UbuntuYakketyFileSystem.addFile("/etc/debian_version", 0,
62593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
63593970f1SMichal Gorny   UbuntuYakketyFileSystem.addFile("/etc/lsb-release", 0,
64593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
65593970f1SMichal Gorny                                        "DISTRIB_RELEASE=16.10\n"
66593970f1SMichal Gorny                                        "DISTRIB_CODENAME=yakkety\n"
67593970f1SMichal Gorny                                        "DISTRIB_DESCRIPTION=\"Ubuntu 16.10\"\n"));
68593970f1SMichal Gorny   UbuntuYakketyFileSystem.addFile("/etc/os-release", 0,
69593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
70593970f1SMichal Gorny                                        "VERSION=\"16.10 (Yakkety Yak)\"\n"
71593970f1SMichal Gorny                                        "ID=ubuntu\n"
72593970f1SMichal Gorny                                        "ID_LIKE=debian\n"
73593970f1SMichal Gorny                                        "PRETTY_NAME=\"Ubuntu 16.10\"\n"
74593970f1SMichal Gorny                                        "VERSION_ID=\"16.10\"\n"
75593970f1SMichal Gorny                                        "HOME_URL=\"http://www.ubuntu.com/\"\n"
76593970f1SMichal Gorny                                        "SUPPORT_URL=\"http://help.ubuntu.com/\"\n"
77593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n"
78593970f1SMichal Gorny                                        "PRIVACY_POLICY_URL=\"http://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n"
79593970f1SMichal Gorny                                        "VERSION_CODENAME=yakkety\n"
80593970f1SMichal Gorny                                        "UBUNTU_CODENAME=yakkety\n"));
81593970f1SMichal Gorny 
821abd4c94SAlexandre Ganea   Distro UbuntuYakkety{UbuntuYakketyFileSystem, llvm::Triple("unknown-pc-linux")};
83593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::UbuntuYakkety), UbuntuYakkety);
84593970f1SMichal Gorny   ASSERT_TRUE(UbuntuYakkety.IsUbuntu());
85593970f1SMichal Gorny   ASSERT_FALSE(UbuntuYakkety.IsRedhat());
86593970f1SMichal Gorny   ASSERT_FALSE(UbuntuYakkety.IsOpenSUSE());
87593970f1SMichal Gorny   ASSERT_FALSE(UbuntuYakkety.IsDebian());
88f241228aSMichal Gorny   ASSERT_FALSE(UbuntuYakkety.IsGentoo());
89593970f1SMichal Gorny }
90593970f1SMichal Gorny 
TEST(DistroTest,DetectRedhat)91593970f1SMichal Gorny TEST(DistroTest, DetectRedhat) {
92fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem Fedora25FileSystem;
93593970f1SMichal Gorny   Fedora25FileSystem.addFile("/etc/system-release-cpe", 0,
94593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("cpe:/o:fedoraproject:fedora:25\n"));
95593970f1SMichal Gorny   // Both files are symlinks to fedora-release.
96593970f1SMichal Gorny   Fedora25FileSystem.addFile("/etc/system-release", 0,
97593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
98593970f1SMichal Gorny   Fedora25FileSystem.addFile("/etc/redhat-release", 0,
99593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
100593970f1SMichal Gorny   Fedora25FileSystem.addFile("/etc/fedora-release", 0,
101593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
102593970f1SMichal Gorny   Fedora25FileSystem.addFile("/etc/os-release", 0,
103593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=Fedora\n"
104593970f1SMichal Gorny                                        "VERSION=\"25 (Twenty Five)\"\n"
105593970f1SMichal Gorny                                        "ID=fedora\n"
106593970f1SMichal Gorny                                        "VERSION_ID=25\n"
107593970f1SMichal Gorny                                        "PRETTY_NAME=\"Fedora 25 (Twenty Five)\"\n"
108593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;34\"\n"
109593970f1SMichal Gorny                                        "CPE_NAME=\"cpe:/o:fedoraproject:fedora:25\"\n"
110593970f1SMichal Gorny                                        "HOME_URL=\"https://fedoraproject.org/\"\n"
111593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugzilla.redhat.com/\"\n"
112593970f1SMichal Gorny                                        "REDHAT_BUGZILLA_PRODUCT=\"Fedora\"\n"
113593970f1SMichal Gorny                                        "REDHAT_BUGZILLA_PRODUCT_VERSION=25\n"
114593970f1SMichal Gorny                                        "REDHAT_SUPPORT_PRODUCT=\"Fedora\"\n"
115593970f1SMichal Gorny                                        "REDHAT_SUPPORT_PRODUCT_VERSION=25\n"
116593970f1SMichal Gorny                                        "PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy\n"));
1171abd4c94SAlexandre Ganea   Distro Fedora25{Fedora25FileSystem, llvm::Triple("unknown-pc-linux")};
118593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::Fedora), Fedora25);
119593970f1SMichal Gorny   ASSERT_FALSE(Fedora25.IsUbuntu());
120593970f1SMichal Gorny   ASSERT_TRUE(Fedora25.IsRedhat());
121593970f1SMichal Gorny   ASSERT_FALSE(Fedora25.IsOpenSUSE());
122593970f1SMichal Gorny   ASSERT_FALSE(Fedora25.IsDebian());
123f241228aSMichal Gorny   ASSERT_FALSE(Fedora25.IsGentoo());
124593970f1SMichal Gorny 
125fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem CentOS7FileSystem;
126593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/system-release-cpe", 0,
127593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("cpe:/o:centos:centos:7\n"));
128593970f1SMichal Gorny   // Both files are symlinks to centos-release.
129593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/system-release", 0,
130593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
131593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/redhat-release", 0,
132593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
133593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/centos-release", 0,
134593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
135593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/centos-release-upstream", 0,
136593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("Derived from Red Hat Enterprise Linux 7.2 (Source)\n"));
137593970f1SMichal Gorny   CentOS7FileSystem.addFile("/etc/os-release", 0,
138593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"CentOS Linux\"\n"
139593970f1SMichal Gorny                                        "VERSION=\"7 (Core)\"\n"
140593970f1SMichal Gorny                                        "ID=\"centos\"\n"
141593970f1SMichal Gorny                                        "ID_LIKE=\"rhel fedora\"\n"
142593970f1SMichal Gorny                                        "VERSION_ID=\"7\"\n"
143593970f1SMichal Gorny                                        "PRETTY_NAME=\"CentOS Linux 7 (Core)\"\n"
144593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;31\"\n"
145593970f1SMichal Gorny                                        "CPE_NAME=\"cpe:/o:centos:centos:7\"\n"
146593970f1SMichal Gorny                                        "HOME_URL=\"https://www.centos.org/\"\n"
147593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.centos.org/\"\n"
148593970f1SMichal Gorny                                        "\n"
149593970f1SMichal Gorny                                        "CENTOS_MANTISBT_PROJECT=\"CentOS-7\"\n"
150593970f1SMichal Gorny                                        "CENTOS_MANTISBT_PROJECT_VERSION=\"7\"\n"
151593970f1SMichal Gorny                                        "REDHAT_SUPPORT_PRODUCT=\"centos\"\n"
152593970f1SMichal Gorny                                        "REDHAT_SUPPORT_PRODUCT_VERSION=\"7\"\n"));
153593970f1SMichal Gorny 
1541abd4c94SAlexandre Ganea   Distro CentOS7{CentOS7FileSystem, llvm::Triple("unknown-pc-linux")};
155593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::RHEL7), CentOS7);
156593970f1SMichal Gorny   ASSERT_FALSE(CentOS7.IsUbuntu());
157593970f1SMichal Gorny   ASSERT_TRUE(CentOS7.IsRedhat());
158593970f1SMichal Gorny   ASSERT_FALSE(CentOS7.IsOpenSUSE());
159593970f1SMichal Gorny   ASSERT_FALSE(CentOS7.IsDebian());
160f241228aSMichal Gorny   ASSERT_FALSE(CentOS7.IsGentoo());
161593970f1SMichal Gorny }
162593970f1SMichal Gorny 
TEST(DistroTest,DetectOpenSUSE)163593970f1SMichal Gorny TEST(DistroTest, DetectOpenSUSE) {
164fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem OpenSUSELeap421FileSystem;
165593970f1SMichal Gorny   OpenSUSELeap421FileSystem.addFile("/etc/SuSE-release", 0,
166593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("openSUSE 42.1 (x86_64)\n"
167593970f1SMichal Gorny                                        "VERSION = 42.1\n"
168593970f1SMichal Gorny                                        "CODENAME = Malachite\n"
169593970f1SMichal Gorny                                        "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));
170593970f1SMichal Gorny   OpenSUSELeap421FileSystem.addFile("/etc/os-release", 0,
171593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"openSUSE Leap\"\n"
172593970f1SMichal Gorny                                        "VERSION=\"42.1\"\n"
173593970f1SMichal Gorny                                        "VERSION_ID=\"42.1\"\n"
174593970f1SMichal Gorny                                        "PRETTY_NAME=\"openSUSE Leap 42.1 (x86_64)\"\n"
175593970f1SMichal Gorny                                        "ID=opensuse\n"
176593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;32\"\n"
177593970f1SMichal Gorny                                        "CPE_NAME=\"cpe:/o:opensuse:opensuse:42.1\"\n"
178593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"
179593970f1SMichal Gorny                                        "HOME_URL=\"https://opensuse.org/\"\n"
180593970f1SMichal Gorny                                        "ID_LIKE=\"suse\"\n"));
181593970f1SMichal Gorny 
1821abd4c94SAlexandre Ganea   Distro OpenSUSELeap421{OpenSUSELeap421FileSystem, llvm::Triple("unknown-pc-linux")};
183593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSELeap421);
184593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSELeap421.IsUbuntu());
185593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSELeap421.IsRedhat());
186593970f1SMichal Gorny   ASSERT_TRUE(OpenSUSELeap421.IsOpenSUSE());
187593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSELeap421.IsDebian());
188f241228aSMichal Gorny   ASSERT_FALSE(OpenSUSELeap421.IsGentoo());
189593970f1SMichal Gorny 
190fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem OpenSUSE132FileSystem;
191593970f1SMichal Gorny   OpenSUSE132FileSystem.addFile("/etc/SuSE-release", 0,
192593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("openSUSE 13.2 (x86_64)\n"
193593970f1SMichal Gorny                                        "VERSION = 13.2\n"
194593970f1SMichal Gorny                                        "CODENAME = Harlequin\n"
195593970f1SMichal Gorny                                        "# /etc/SuSE-release is deprecated and will be removed in the future, use /etc/os-release instead\n"));
196593970f1SMichal Gorny   OpenSUSE132FileSystem.addFile("/etc/os-release", 0,
197593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=openSUSE\n"
198593970f1SMichal Gorny                                        "VERSION=\"13.2 (Harlequin)\"\n"
199593970f1SMichal Gorny                                        "VERSION_ID=\"13.2\"\n"
200593970f1SMichal Gorny                                        "PRETTY_NAME=\"openSUSE 13.2 (Harlequin) (x86_64)\"\n"
201593970f1SMichal Gorny                                        "ID=opensuse\n"
202593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;32\"\n"
203593970f1SMichal Gorny                                        "CPE_NAME=\"cpe:/o:opensuse:opensuse:13.2\"\n"
204593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.opensuse.org\"\n"
205593970f1SMichal Gorny                                        "HOME_URL=\"https://opensuse.org/\"\n"
206593970f1SMichal Gorny                                        "ID_LIKE=\"suse\"\n"));
207593970f1SMichal Gorny 
2081abd4c94SAlexandre Ganea   Distro OpenSUSE132{OpenSUSE132FileSystem, llvm::Triple("unknown-pc-linux")};
209593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::OpenSUSE), OpenSUSE132);
210593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSE132.IsUbuntu());
211593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSE132.IsRedhat());
212593970f1SMichal Gorny   ASSERT_TRUE(OpenSUSE132.IsOpenSUSE());
213593970f1SMichal Gorny   ASSERT_FALSE(OpenSUSE132.IsDebian());
214f241228aSMichal Gorny   ASSERT_FALSE(OpenSUSE132.IsGentoo());
215593970f1SMichal Gorny 
216fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem SLES10FileSystem;
217593970f1SMichal Gorny   SLES10FileSystem.addFile("/etc/SuSE-release", 0,
218593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("SUSE Linux Enterprise Server 10 (x86_64)\n"
219593970f1SMichal Gorny                                        "VERSION = 10\n"
220593970f1SMichal Gorny                                        "PATCHLEVEL = 4\n"));
221593970f1SMichal Gorny   SLES10FileSystem.addFile("/etc/lsb_release", 0,
222593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("LSB_VERSION=\"core-2.0-noarch:core-3.0-noarch:core-2.0-x86_64:core-3.0-x86_64\"\n"));
223593970f1SMichal Gorny 
224593970f1SMichal Gorny   // SLES10 is unsupported and therefore evaluates to unknown
2251abd4c94SAlexandre Ganea   Distro SLES10{SLES10FileSystem, llvm::Triple("unknown-pc-linux")};
226593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::UnknownDistro), SLES10);
227593970f1SMichal Gorny   ASSERT_FALSE(SLES10.IsUbuntu());
228593970f1SMichal Gorny   ASSERT_FALSE(SLES10.IsRedhat());
229593970f1SMichal Gorny   ASSERT_FALSE(SLES10.IsOpenSUSE());
230593970f1SMichal Gorny   ASSERT_FALSE(SLES10.IsDebian());
231f241228aSMichal Gorny   ASSERT_FALSE(SLES10.IsGentoo());
232593970f1SMichal Gorny }
233593970f1SMichal Gorny 
TEST(DistroTest,DetectDebian)234593970f1SMichal Gorny TEST(DistroTest, DetectDebian) {
235fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem DebianJessieFileSystem;
236593970f1SMichal Gorny   DebianJessieFileSystem.addFile("/etc/debian_version", 0,
237593970f1SMichal Gorny                                  llvm::MemoryBuffer::getMemBuffer("8.6\n"));
238593970f1SMichal Gorny   DebianJessieFileSystem.addFile("/etc/os-release", 0,
239593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n"
240593970f1SMichal Gorny                                        "NAME=\"Debian GNU/Linux\"\n"
241593970f1SMichal Gorny                                        "VERSION_ID=\"8\"\n"
242593970f1SMichal Gorny                                        "VERSION=\"8 (jessie)\"\n"
243593970f1SMichal Gorny                                        "ID=debian\n"
244593970f1SMichal Gorny                                        "HOME_URL=\"http://www.debian.org/\"\n"
245593970f1SMichal Gorny                                        "SUPPORT_URL=\"http://www.debian.org/support\"\n"
246593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));
247593970f1SMichal Gorny 
2481abd4c94SAlexandre Ganea   Distro DebianJessie{DebianJessieFileSystem, llvm::Triple("unknown-pc-linux")};
249593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::DebianJessie), DebianJessie);
250593970f1SMichal Gorny   ASSERT_FALSE(DebianJessie.IsUbuntu());
251593970f1SMichal Gorny   ASSERT_FALSE(DebianJessie.IsRedhat());
252593970f1SMichal Gorny   ASSERT_FALSE(DebianJessie.IsOpenSUSE());
253593970f1SMichal Gorny   ASSERT_TRUE(DebianJessie.IsDebian());
254f241228aSMichal Gorny   ASSERT_FALSE(DebianJessie.IsGentoo());
255593970f1SMichal Gorny 
256fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem DebianStretchSidFileSystem;
257593970f1SMichal Gorny   DebianStretchSidFileSystem.addFile("/etc/debian_version", 0,
258593970f1SMichal Gorny                                  llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
259593970f1SMichal Gorny   DebianStretchSidFileSystem.addFile("/etc/os-release", 0,
260593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux stretch/sid\"\n"
261593970f1SMichal Gorny                                        "NAME=\"Debian GNU/Linux\"\n"
262593970f1SMichal Gorny                                        "ID=debian\n"
263593970f1SMichal Gorny                                        "HOME_URL=\"http://www.debian.org/\"\n"
264593970f1SMichal Gorny                                        "SUPPORT_URL=\"http://www.debian.org/support\"\n"
265593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n"));
266593970f1SMichal Gorny 
2671abd4c94SAlexandre Ganea   Distro DebianStretchSid{DebianStretchSidFileSystem, llvm::Triple("unknown-pc-linux")};
268593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::DebianStretch), DebianStretchSid);
269593970f1SMichal Gorny   ASSERT_FALSE(DebianStretchSid.IsUbuntu());
270593970f1SMichal Gorny   ASSERT_FALSE(DebianStretchSid.IsRedhat());
271593970f1SMichal Gorny   ASSERT_FALSE(DebianStretchSid.IsOpenSUSE());
272593970f1SMichal Gorny   ASSERT_TRUE(DebianStretchSid.IsDebian());
273f241228aSMichal Gorny   ASSERT_FALSE(DebianStretchSid.IsGentoo());
274593970f1SMichal Gorny }
275593970f1SMichal Gorny 
TEST(DistroTest,DetectExherbo)276593970f1SMichal Gorny TEST(DistroTest, DetectExherbo) {
277fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem ExherboFileSystem;
278593970f1SMichal Gorny   ExherboFileSystem.addFile("/etc/os-release", 0,
279593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"Exherbo\"\n"
280593970f1SMichal Gorny                                        "PRETTY_NAME=\"Exherbo Linux\"\n"
281931a3aa9SFangrui Song                                        "ID=exherbo\n"
282593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;32\"\n"
283593970f1SMichal Gorny                                        "HOME_URL=\"https://www.exherbo.org/\"\n"
284593970f1SMichal Gorny                                        "SUPPORT_URL=\"irc://irc.freenode.net/#exherbo\"\n"
285593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.exherbo.org/\"\n"));
286593970f1SMichal Gorny 
2871abd4c94SAlexandre Ganea   Distro Exherbo{ExherboFileSystem, llvm::Triple("unknown-pc-linux")};
288593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::Exherbo), Exherbo);
289593970f1SMichal Gorny   ASSERT_FALSE(Exherbo.IsUbuntu());
290593970f1SMichal Gorny   ASSERT_FALSE(Exherbo.IsRedhat());
291593970f1SMichal Gorny   ASSERT_FALSE(Exherbo.IsOpenSUSE());
292593970f1SMichal Gorny   ASSERT_FALSE(Exherbo.IsDebian());
293f241228aSMichal Gorny   ASSERT_FALSE(Exherbo.IsGentoo());
294593970f1SMichal Gorny }
295593970f1SMichal Gorny 
TEST(DistroTest,DetectArchLinux)296593970f1SMichal Gorny TEST(DistroTest, DetectArchLinux) {
297fc51490bSJonas Devlieghere   llvm::vfs::InMemoryFileSystem ArchLinuxFileSystem;
298593970f1SMichal Gorny   ArchLinuxFileSystem.addFile("/etc/arch-release", 0, // (empty)
299593970f1SMichal Gorny                                  llvm::MemoryBuffer::getMemBuffer(""));
300593970f1SMichal Gorny   ArchLinuxFileSystem.addFile("/etc/os-release", 0,
301593970f1SMichal Gorny       llvm::MemoryBuffer::getMemBuffer("NAME=\"Arch Linux\"\n"
302593970f1SMichal Gorny                                        "ID=arch\n"
303593970f1SMichal Gorny                                        "PRETTY_NAME=\"Arch Linux\"\n"
304593970f1SMichal Gorny                                        "ANSI_COLOR=\"0;36\"\n"
305593970f1SMichal Gorny                                        "HOME_URL=\"https://www.archlinux.org/\"\n"
306593970f1SMichal Gorny                                        "SUPPORT_URL=\"https://bbs.archlinux.org/\"\n"
307593970f1SMichal Gorny                                        "BUG_REPORT_URL=\"https://bugs.archlinux.org/\"\n"));
308593970f1SMichal Gorny 
3091abd4c94SAlexandre Ganea   Distro ArchLinux{ArchLinuxFileSystem, llvm::Triple("unknown-pc-linux")};
310593970f1SMichal Gorny   ASSERT_EQ(Distro(Distro::ArchLinux), ArchLinux);
311593970f1SMichal Gorny   ASSERT_FALSE(ArchLinux.IsUbuntu());
312593970f1SMichal Gorny   ASSERT_FALSE(ArchLinux.IsRedhat());
313593970f1SMichal Gorny   ASSERT_FALSE(ArchLinux.IsOpenSUSE());
314593970f1SMichal Gorny   ASSERT_FALSE(ArchLinux.IsDebian());
315f241228aSMichal Gorny   ASSERT_FALSE(ArchLinux.IsGentoo());
316f241228aSMichal Gorny }
317f241228aSMichal Gorny 
TEST(DistroTest,DetectGentoo)318f241228aSMichal Gorny TEST(DistroTest, DetectGentoo) {
319f241228aSMichal Gorny   llvm::vfs::InMemoryFileSystem GentooFileSystem;
320f241228aSMichal Gorny   GentooFileSystem.addFile(
321f241228aSMichal Gorny       "/etc/gentoo-release", 0,
322f241228aSMichal Gorny       llvm::MemoryBuffer::getMemBuffer("Gentoo Base System release 2.6"));
323f241228aSMichal Gorny   GentooFileSystem.addFile(
324f241228aSMichal Gorny       "/etc/os-release", 0,
325f241228aSMichal Gorny       llvm::MemoryBuffer::getMemBuffer(
326f241228aSMichal Gorny           "NAME=Gentoo\n"
327f241228aSMichal Gorny           "ID=gentoo\n"
328f241228aSMichal Gorny           "PRETTY_NAME=\"Gentoo/Linux\"\n"
329f241228aSMichal Gorny           "ANSI_COLOR=\"1;32\"\n"
330f241228aSMichal Gorny           "HOME_URL=\"https://www.gentoo.org/\"\n"
331f241228aSMichal Gorny           "SUPPORT_URL=\"https://www.gentoo.org/support/\"\n"
332f241228aSMichal Gorny           "BUG_REPORT_URL=\"https://bugs.gentoo.org/\"\n"));
333f241228aSMichal Gorny 
3341abd4c94SAlexandre Ganea   Distro Gentoo{GentooFileSystem, llvm::Triple("unknown-pc-linux")};
335f241228aSMichal Gorny   ASSERT_EQ(Distro(Distro::Gentoo), Gentoo);
336f241228aSMichal Gorny   ASSERT_FALSE(Gentoo.IsUbuntu());
337f241228aSMichal Gorny   ASSERT_FALSE(Gentoo.IsRedhat());
338f241228aSMichal Gorny   ASSERT_FALSE(Gentoo.IsOpenSUSE());
339f241228aSMichal Gorny   ASSERT_FALSE(Gentoo.IsDebian());
340f241228aSMichal Gorny   ASSERT_TRUE(Gentoo.IsGentoo());
341593970f1SMichal Gorny }
342593970f1SMichal Gorny 
TEST(DistroTest,DetectWindowsAndCrossCompile)3431abd4c94SAlexandre Ganea TEST(DistroTest, DetectWindowsAndCrossCompile) {
3441abd4c94SAlexandre Ganea 
3451abd4c94SAlexandre Ganea   class CountingFileSystem : public llvm::vfs::ProxyFileSystem {
3461abd4c94SAlexandre Ganea   public:
3471abd4c94SAlexandre Ganea     CountingFileSystem() : ProxyFileSystem(llvm::vfs::getRealFileSystem()) {}
3481abd4c94SAlexandre Ganea 
3491abd4c94SAlexandre Ganea     llvm::ErrorOr<llvm::vfs::Status> status(const llvm::Twine &Path) override {
3501abd4c94SAlexandre Ganea       ++Count;
3511abd4c94SAlexandre Ganea       return llvm::vfs::ProxyFileSystem::status(Path);
3521abd4c94SAlexandre Ganea     }
3531abd4c94SAlexandre Ganea 
3541abd4c94SAlexandre Ganea     llvm::ErrorOr<std::unique_ptr<llvm::vfs::File>>
3551abd4c94SAlexandre Ganea     openFileForRead(const llvm::Twine &Path) override {
3561abd4c94SAlexandre Ganea       ++Count;
3571abd4c94SAlexandre Ganea       return llvm::vfs::ProxyFileSystem::openFileForRead(Path);
3581abd4c94SAlexandre Ganea     }
3591abd4c94SAlexandre Ganea 
3601abd4c94SAlexandre Ganea     unsigned Count{};
3611abd4c94SAlexandre Ganea   };
3621abd4c94SAlexandre Ganea 
363e22e2774SPaul Robinson   llvm::Triple Host(llvm::sys::getProcessTriple());
364e22e2774SPaul Robinson   if (!Host.isOSWindows())
365e22e2774SPaul Robinson     GTEST_SKIP();
366e22e2774SPaul Robinson 
3671abd4c94SAlexandre Ganea   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RFS =
3681abd4c94SAlexandre Ganea       llvm::vfs::getRealFileSystem();
3691abd4c94SAlexandre Ganea 
3701abd4c94SAlexandre Ganea   CountingFileSystem CFileSystem;
3711abd4c94SAlexandre Ganea   Distro LinuxDistro{CFileSystem, llvm::Triple("unknown-pc-linux")};
3721abd4c94SAlexandre Ganea   ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxDistro);
3731abd4c94SAlexandre Ganea   ASSERT_GT(CFileSystem.Count, 0U);
3741abd4c94SAlexandre Ganea 
3751abd4c94SAlexandre Ganea   Distro WinDistro{CFileSystem, llvm::Triple("unknown-pc-windows")};
3761abd4c94SAlexandre Ganea   ASSERT_EQ(Distro(Distro::UnknownDistro), WinDistro);
3771abd4c94SAlexandre Ganea   ASSERT_GT(CFileSystem.Count, 0U);
3781abd4c94SAlexandre Ganea 
3791abd4c94SAlexandre Ganea   // When running on Windows along with a real file system, ensure that no
3801abd4c94SAlexandre Ganea   // distro is returned if targeting Linux
3811abd4c94SAlexandre Ganea   Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};
3821abd4c94SAlexandre Ganea   ASSERT_EQ(Distro(Distro::UnknownDistro), LinuxRealDistro);
3831abd4c94SAlexandre Ganea 
3841abd4c94SAlexandre Ganea   Distro WinRealDistro{*RFS, llvm::Triple("unknown-pc-windows")};
3851abd4c94SAlexandre Ganea   ASSERT_EQ(Distro(Distro::UnknownDistro), WinRealDistro);
3861abd4c94SAlexandre Ganea }
3871abd4c94SAlexandre Ganea 
TEST(DistroTest,DetectLinux)388e22e2774SPaul Robinson TEST(DistroTest, DetectLinux) {
389e22e2774SPaul Robinson   llvm::Triple Host(llvm::sys::getProcessTriple());
390e22e2774SPaul Robinson   if (!Host.isOSLinux())
391e22e2774SPaul Robinson     GTEST_SKIP();
392e22e2774SPaul Robinson 
393e22e2774SPaul Robinson   // When running on Linux, check if the distro is the same as the host when
394e22e2774SPaul Robinson   // targeting Linux
395e22e2774SPaul Robinson   llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem> RFS =
396e22e2774SPaul Robinson       llvm::vfs::getRealFileSystem();
397e22e2774SPaul Robinson   Distro HostDistro{*RFS, Host};
398e22e2774SPaul Robinson   Distro LinuxRealDistro{*RFS, llvm::Triple("unknown-pc-linux")};
399e22e2774SPaul Robinson   ASSERT_EQ(HostDistro, LinuxRealDistro);
400e22e2774SPaul Robinson }
401e22e2774SPaul Robinson 
402593970f1SMichal Gorny } // end anonymous namespace
403