mgorny retitled this revision from "[Driver] Add unit tests for DetectDistro()" 
to "[Driver] Add unit tests for Distro detection".
mgorny updated the summary for this revision.
mgorny updated this revision to Diff 78494.
mgorny added a comment.

Ok, here are the tests updated for the new API. I'll try to look into adding 
more distributions later today.


https://reviews.llvm.org/D25869

Files:
  unittests/Driver/CMakeLists.txt
  unittests/Driver/DistroTest.cpp

Index: unittests/Driver/DistroTest.cpp
===================================================================
--- /dev/null
+++ unittests/Driver/DistroTest.cpp
@@ -0,0 +1,195 @@
+//===- unittests/Driver/DistroTest.cpp --- ToolChains tests ---------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+//
+// Unit tests for Distro detection.
+//
+//===----------------------------------------------------------------------===//
+
+#include "clang/Driver/Distro.h"
+#include "clang/Basic/VirtualFileSystem.h"
+#include "llvm/Support/raw_ostream.h"
+#include "gtest/gtest.h"
+using namespace clang;
+using namespace clang::driver;
+
+namespace {
+
+// The tests include all release-related files for each distribution
+// in the VFS, in order to make sure that earlier tests do not
+// accidentally result in incorrect distribution guess.
+
+TEST(DistroTest, DetectUbuntu) {
+  vfs::InMemoryFileSystem UbuntuTrustyFileSystem;
+  // Ubuntu uses Debian Sid version.
+  UbuntuTrustyFileSystem.addFile("/etc/debian_version", 0,
+      llvm::MemoryBuffer::getMemBuffer("jessie/sid\n"));
+  UbuntuTrustyFileSystem.addFile("/etc/lsb-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
+                                       "DISTRIB_RELEASE=14.04\n"
+                                       "DISTRIB_CODENAME=trusty\n"
+                                       "DISTRIB_DESCRIPTION=\"Ubuntu 14.04 LTS\"\n"));
+  UbuntuTrustyFileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
+                                       "VERSION=\"14.04, Trusty Tahr\"\n"
+                                       "ID=ubuntu\n"
+                                       "ID_LIKE=debian\n"
+                                       "PRETTY_NAME=\"Ubuntu 14.04 LTS\"\n"
+                                       "VERSION_ID=\"14.04\"\n"
+                                       "HOME_URL=\"http://www.ubuntu.com/\"\n";
+                                       "SUPPORT_URL=\"http://help.ubuntu.com/\"\n";
+                                       "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n";));
+
+  Distro UbuntuTrusty{UbuntuTrustyFileSystem};
+  ASSERT_EQ(Distro(Distro::UbuntuTrusty), UbuntuTrusty);
+  ASSERT_TRUE(UbuntuTrusty.IsUbuntu());
+  ASSERT_FALSE(UbuntuTrusty.IsRedhat());
+  ASSERT_FALSE(UbuntuTrusty.IsOpenSUSE());
+  ASSERT_FALSE(UbuntuTrusty.IsDebian());
+
+  vfs::InMemoryFileSystem UbuntuYakketyFileSystem;
+  UbuntuYakketyFileSystem.addFile("/etc/debian_version", 0,
+      llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
+  UbuntuYakketyFileSystem.addFile("/etc/lsb-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("DISTRIB_ID=Ubuntu\n"
+                                       "DISTRIB_RELEASE=16.10\n"
+                                       "DISTRIB_CODENAME=yakkety\n"
+                                       "DISTRIB_DESCRIPTION=\"Ubuntu 16.10\"\n"));
+  UbuntuYakketyFileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("NAME=\"Ubuntu\"\n"
+                                       "VERSION=\"16.10 (Yakkety Yak)\"\n"
+                                       "ID=ubuntu\n"
+                                       "ID_LIKE=debian\n"
+                                       "PRETTY_NAME=\"Ubuntu 16.10\"\n"
+                                       "VERSION_ID=\"16.10\"\n"
+                                       "HOME_URL=\"http://www.ubuntu.com/\"\n";
+                                       "SUPPORT_URL=\"http://help.ubuntu.com/\"\n";
+                                       "BUG_REPORT_URL=\"http://bugs.launchpad.net/ubuntu/\"\n";
+                                       "PRIVACY_POLICY_URL=\"http://www.ubuntu.com/legal/terms-and-policies/privacy-policy\"\n";
+                                       "VERSION_CODENAME=yakkety\n"
+                                       "UBUNTU_CODENAME=yakkety\n"));
+
+  Distro UbuntuYakkety{UbuntuYakketyFileSystem};
+  ASSERT_EQ(Distro(Distro::UbuntuYakkety), UbuntuYakkety);
+  ASSERT_TRUE(UbuntuYakkety.IsUbuntu());
+  ASSERT_FALSE(UbuntuYakkety.IsRedhat());
+  ASSERT_FALSE(UbuntuYakkety.IsOpenSUSE());
+  ASSERT_FALSE(UbuntuYakkety.IsDebian());
+}
+
+TEST(DistroTest, DetectRedhat) {
+  vfs::InMemoryFileSystem Fedora25FileSystem;
+  Fedora25FileSystem.addFile("/etc/system-release-cpe", 0,
+      llvm::MemoryBuffer::getMemBuffer("cpe:/o:fedoraproject:fedora:25\n"));
+  // Both files are symlinks to fedora-release.
+  Fedora25FileSystem.addFile("/etc/system-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
+  Fedora25FileSystem.addFile("/etc/redhat-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
+  Fedora25FileSystem.addFile("/etc/fedora-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("Fedora release 25 (Twenty Five)\n"));
+  Fedora25FileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("NAME=Fedora\n"
+                                       "VERSION=\"25 (Twenty Five)\"\n"
+                                       "ID=fedora\n"
+                                       "VERSION_ID=25\n"
+                                       "PRETTY_NAME=\"Fedora 25 (Twenty Five)\"\n"
+                                       "ANSI_COLOR=\"0;34\"\n"
+                                       "CPE_NAME=\"cpe:/o:fedoraproject:fedora:25\"\n"
+                                       "HOME_URL=\"https://fedoraproject.org/\"\n";
+                                       "BUG_REPORT_URL=\"https://bugzilla.redhat.com/\"\n";
+                                       "REDHAT_BUGZILLA_PRODUCT=\"Fedora\"\n"
+                                       "REDHAT_BUGZILLA_PRODUCT_VERSION=25\n"
+                                       "REDHAT_SUPPORT_PRODUCT=\"Fedora\"\n"
+                                       "REDHAT_SUPPORT_PRODUCT_VERSION=25\n"
+                                       "PRIVACY_POLICY_URL=https://fedoraproject.org/wiki/Legal:PrivacyPolicy\n";));
+  Distro Fedora25{Fedora25FileSystem};
+  ASSERT_EQ(Distro(Distro::Fedora), Fedora25);
+  ASSERT_FALSE(Fedora25.IsUbuntu());
+  ASSERT_TRUE(Fedora25.IsRedhat());
+  ASSERT_FALSE(Fedora25.IsOpenSUSE());
+  ASSERT_FALSE(Fedora25.IsDebian());
+
+  vfs::InMemoryFileSystem CentOS7FileSystem;
+  CentOS7FileSystem.addFile("/etc/system-release-cpe", 0,
+      llvm::MemoryBuffer::getMemBuffer("cpe:/o:centos:centos:7\n"));
+  // Both files are symlinks to centos-release.
+  CentOS7FileSystem.addFile("/etc/system-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
+  CentOS7FileSystem.addFile("/etc/redhat-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
+  CentOS7FileSystem.addFile("/etc/centos-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("CentOS Linux release 7.2.1511 (Core) \n"));
+  CentOS7FileSystem.addFile("/etc/centos-release-upstream", 0,
+      llvm::MemoryBuffer::getMemBuffer("Derived from Red Hat Enterprise Linux 7.2 (Source)\n"));
+  CentOS7FileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("NAME=\"CentOS Linux\"\n"
+                                       "VERSION=\"7 (Core)\"\n"
+                                       "ID=\"centos\"\n"
+                                       "ID_LIKE=\"rhel fedora\"\n"
+                                       "VERSION_ID=\"7\"\n"
+                                       "PRETTY_NAME=\"CentOS Linux 7 (Core)\"\n"
+                                       "ANSI_COLOR=\"0;31\"\n"
+                                       "CPE_NAME=\"cpe:/o:centos:centos:7\"\n"
+                                       "HOME_URL=\"https://www.centos.org/\"\n";
+                                       "BUG_REPORT_URL=\"https://bugs.centos.org/\"\n";
+                                       "\n"
+                                       "CENTOS_MANTISBT_PROJECT=\"CentOS-7\"\n"
+                                       "CENTOS_MANTISBT_PROJECT_VERSION=\"7\"\n"
+                                       "REDHAT_SUPPORT_PRODUCT=\"centos\"\n"
+                                       "REDHAT_SUPPORT_PRODUCT_VERSION=\"7\"\n"));
+
+  Distro CentOS7{CentOS7FileSystem};
+  ASSERT_EQ(Distro(Distro::RHEL7), CentOS7);
+  ASSERT_FALSE(CentOS7.IsUbuntu());
+  ASSERT_TRUE(CentOS7.IsRedhat());
+  ASSERT_FALSE(CentOS7.IsOpenSUSE());
+  ASSERT_FALSE(CentOS7.IsDebian());
+}
+
+TEST(DistroTest, DetectDebian) {
+  vfs::InMemoryFileSystem DebianJessieFileSystem;
+  DebianJessieFileSystem.addFile("/etc/debian_version", 0,
+                                 llvm::MemoryBuffer::getMemBuffer("8.6\n"));
+  DebianJessieFileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux 8 (jessie)\"\n"
+                                       "NAME=\"Debian GNU/Linux\"\n"
+                                       "VERSION_ID=\"8\"\n"
+                                       "VERSION=\"8 (jessie)\"\n"
+                                       "ID=debian\n"
+                                       "HOME_URL=\"http://www.debian.org/\"\n";
+                                       "SUPPORT_URL=\"http://www.debian.org/support\"\n";
+                                       "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n";));
+
+  Distro DebianJessie{DebianJessieFileSystem};
+  ASSERT_EQ(Distro(Distro::DebianJessie), DebianJessie);
+  ASSERT_FALSE(DebianJessie.IsUbuntu());
+  ASSERT_FALSE(DebianJessie.IsRedhat());
+  ASSERT_FALSE(DebianJessie.IsOpenSUSE());
+  ASSERT_TRUE(DebianJessie.IsDebian());
+
+  vfs::InMemoryFileSystem DebianStretchSidFileSystem;
+  DebianStretchSidFileSystem.addFile("/etc/debian_version", 0,
+                                 llvm::MemoryBuffer::getMemBuffer("stretch/sid\n"));
+  DebianStretchSidFileSystem.addFile("/etc/os-release", 0,
+      llvm::MemoryBuffer::getMemBuffer("PRETTY_NAME=\"Debian GNU/Linux stretch/sid\"\n"
+                                       "NAME=\"Debian GNU/Linux\"\n"
+                                       "ID=debian\n"
+                                       "HOME_URL=\"http://www.debian.org/\"\n";
+                                       "SUPPORT_URL=\"http://www.debian.org/support\"\n";
+                                       "BUG_REPORT_URL=\"https://bugs.debian.org/\"\n";));
+
+  Distro DebianStretchSid{DebianStretchSidFileSystem};
+  ASSERT_EQ(Distro(Distro::DebianStretch), DebianStretchSid);
+  ASSERT_FALSE(DebianStretchSid.IsUbuntu());
+  ASSERT_FALSE(DebianStretchSid.IsRedhat());
+  ASSERT_FALSE(DebianStretchSid.IsOpenSUSE());
+  ASSERT_TRUE(DebianStretchSid.IsDebian());
+}
+
+} // end anonymous namespace
Index: unittests/Driver/CMakeLists.txt
===================================================================
--- unittests/Driver/CMakeLists.txt
+++ unittests/Driver/CMakeLists.txt
@@ -3,6 +3,7 @@
   )
 
 add_clang_unittest(ClangDriverTests
+  DistroTest.cpp
   ToolChainTest.cpp
   MultilibTest.cpp
   )
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to