mgorny created this revision. mgorny added reviewers: bruno, bkramer, rafael. mgorny added a subscriber: cfe-commits. Herald added a subscriber: beanz.
Add a set of unit tests for the DetectDistro() function in Driver. Make the function itself (and the enum) visible in the library for the tests. The tests use an in-memory virtual filesystems resembling release files for various distributions supported. All release files are provided (not only the ones directly used) in order to guarantee that one of the rules will not mistakenly recognize the distribution incorrectly due to the additional files (e.g. Ubuntu as Debian). https://reviews.llvm.org/D25869 Files: lib/Driver/ToolChains.cpp lib/Driver/ToolChains.h unittests/Driver/CMakeLists.txt unittests/Driver/ToolChainsTest.cpp
Index: unittests/Driver/ToolChainsTest.cpp =================================================================== --- /dev/null +++ unittests/Driver/ToolChainsTest.cpp @@ -0,0 +1,157 @@ +//===- unittests/Driver/ToolChainsTest.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 ToolChains. +// +//===----------------------------------------------------------------------===// + +// FIXME: I presume this is not the correct way of doing this +#include "../lib/Driver/ToolChains.h" +#include "clang/Basic/VirtualFileSystem.h" +#include "llvm/Support/raw_ostream.h" +#include "gtest/gtest.h" +using namespace clang; +using namespace clang::driver::toolchains; + +namespace { + +TEST(ToolChainsTest, DetectDistro) { + // 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. + + 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")); + ASSERT_EQ(UbuntuTrusty, DetectDistro(UbuntuTrustyFileSystem)); + + 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")); + ASSERT_EQ(UbuntuYakkety, DetectDistro(UbuntuYakketyFileSystem)); + + 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")); + ASSERT_EQ(Fedora, DetectDistro(Fedora25FileSystem)); + + 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")); + ASSERT_EQ(RHEL7, DetectDistro(CentOS7FileSystem)); + + 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")); + ASSERT_EQ(DebianJessie, DetectDistro(DebianJessieFileSystem)); + + 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")); + ASSERT_EQ(DebianStretch, DetectDistro(DebianStretchSidFileSystem)); +} + +} // end anonymous namespace Index: unittests/Driver/CMakeLists.txt =================================================================== --- unittests/Driver/CMakeLists.txt +++ unittests/Driver/CMakeLists.txt @@ -5,6 +5,7 @@ add_clang_unittest(ClangDriverTests ToolChainTest.cpp MultilibTest.cpp + ToolChainsTest.cpp ) target_link_libraries(ClangDriverTests Index: lib/Driver/ToolChains.h =================================================================== --- lib/Driver/ToolChains.h +++ lib/Driver/ToolChains.h @@ -817,6 +817,48 @@ Tool *buildLinker() const override; }; +/// Distribution (very bare-bones at the moment). +// (bits needed for unit tests) + +enum Distro { + // NB: Releases of a particular Linux distro should be kept together + // in this enum, because some tests are done by integer comparison against + // the first and last known member in the family, e.g. IsRedHat(). + ArchLinux, + DebianLenny, + DebianSqueeze, + DebianWheezy, + DebianJessie, + DebianStretch, + Exherbo, + RHEL5, + RHEL6, + RHEL7, + Fedora, + OpenSUSE, + UbuntuHardy, + UbuntuIntrepid, + UbuntuJaunty, + UbuntuKarmic, + UbuntuLucid, + UbuntuMaverick, + UbuntuNatty, + UbuntuOneiric, + UbuntuPrecise, + UbuntuQuantal, + UbuntuRaring, + UbuntuSaucy, + UbuntuTrusty, + UbuntuUtopic, + UbuntuVivid, + UbuntuWily, + UbuntuXenial, + UbuntuYakkety, + UnknownDistro +}; + +Distro DetectDistro(vfs::FileSystem &VFS); + class LLVM_LIBRARY_VISIBILITY Linux : public Generic_ELF { public: Linux(const Driver &D, const llvm::Triple &Triple, Index: lib/Driver/ToolChains.cpp =================================================================== --- lib/Driver/ToolChains.cpp +++ lib/Driver/ToolChains.cpp @@ -3799,43 +3799,6 @@ /// Distribution (very bare-bones at the moment). -enum Distro { - // NB: Releases of a particular Linux distro should be kept together - // in this enum, because some tests are done by integer comparison against - // the first and last known member in the family, e.g. IsRedHat(). - ArchLinux, - DebianLenny, - DebianSqueeze, - DebianWheezy, - DebianJessie, - DebianStretch, - Exherbo, - RHEL5, - RHEL6, - RHEL7, - Fedora, - OpenSUSE, - UbuntuHardy, - UbuntuIntrepid, - UbuntuJaunty, - UbuntuKarmic, - UbuntuLucid, - UbuntuMaverick, - UbuntuNatty, - UbuntuOneiric, - UbuntuPrecise, - UbuntuQuantal, - UbuntuRaring, - UbuntuSaucy, - UbuntuTrusty, - UbuntuUtopic, - UbuntuVivid, - UbuntuWily, - UbuntuXenial, - UbuntuYakkety, - UnknownDistro -}; - static bool IsRedhat(enum Distro Distro) { return Distro == Fedora || (Distro >= RHEL5 && Distro <= RHEL7); } @@ -3850,7 +3813,7 @@ return Distro >= UbuntuHardy && Distro <= UbuntuYakkety; } -static Distro DetectDistro(vfs::FileSystem &VFS) { +Distro clang::driver::toolchains::DetectDistro(vfs::FileSystem &VFS) { llvm::ErrorOr<std::unique_ptr<llvm::MemoryBuffer>> File = VFS.getBufferForFile("/etc/lsb-release"); if (File) {
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits