Edit

kc3-lang/angle/src/common/system_utils_unittest.cpp

Branch :

  • Show log

    Commit

  • Author : Shahbaz Youssefi
    Date : 2019-02-25 16:31:57
    Hash : a9f89313
    Message : Reland "Add system util to execute app and retrieve its output" This reverts commit fe14b2e503a5991aeb033836bb4d525508475b52. Reason for revert: failing test is reworked not to run angle_unittests itself, but another binary. Previously, this test was calling angle_unittests itself, but with a different target. On the bots, that was in turn calling angle_unittests with even more arguments, including the addition of a log file location. Under some configurations, this separate process was thus trying to access files that were already opened by the parent process, leading to a test failure. In this CL, a new helper executable is created for the sake of this unittest. > Revert "Add system util to execute app and retrieve its output" > > This reverts commit c63d95525cde8d28963148bb5894456c1d39018d. > > Reason for revert: Test fails on Win7 > > Original change's description: > > Add system util to execute app and retrieve its output > > > > This will be useful to run external applications, such as benchmarks, > > and process their output. > > > > Bug: angleproject:3125 > > Change-Id: Ic13c69f2e034f4b47498fb2f299c62423c355c4a > > Reviewed-on: https://chromium-review.googlesource.com/c/1452534 > > Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> > > Reviewed-by: Jamie Madill <jmadill@google.com> Bug: angleproject:3125, angleproject:3168 Change-Id: I74815750484a79f33c36e0b4f941d4dd98f99aa5 Reviewed-on: https://chromium-review.googlesource.com/c/1487631 Commit-Queue: Shahbaz Youssefi <syoussefi@chromium.org> Reviewed-by: Jamie Madill <jmadill@google.com> Reviewed-by: Yuly Novikov <ynovikov@chromium.org>

  • src/common/system_utils_unittest.cpp
  • //
    // Copyright 2019 The ANGLE Project Authors. All rights reserved.
    // Use of this source code is governed by a BSD-style license that can be
    // found in the LICENSE file.
    
    // system_utils_unittest.cpp: Unit tests for ANGLE's system utility functions
    
    #include "gmock/gmock.h"
    #include "gtest/gtest.h"
    
    #include "common/system_utils.h"
    #include "common/system_utils_unittest_helper.h"
    
    using namespace angle;
    
    namespace
    {
    #if defined(ANGLE_PLATFORM_WINDOWS)
    constexpr char kRunAppHelperExecutable[] = "angle_unittests_helper.exe";
    #else
    constexpr char kRunAppHelperExecutable[] = "angle_unittests_helper";
    #endif
    
    // Transforms various line endings into C/Unix line endings:
    //
    // - A\nB -> A\nB
    // - A\rB -> A\nB
    // - A\r\nB -> A\nB
    std::string NormalizeNewLines(const std::string &str)
    {
        std::string result;
    
        for (size_t i = 0; i < str.size(); ++i)
        {
            if (str[i] == '\r')
            {
                if (i + 1 < str.size() && str[i + 1] == '\n')
                {
                    ++i;
                }
                result += '\n';
            }
            else
            {
                result += str[i];
            }
        }
    
        return result;
    }
    
    // Test getting the executable path
    TEST(SystemUtils, ExecutablePath)
    {
    #if defined(ANGLE_PLATFORM_FUCHSIA)
        // TODO: fuchsia support. http://anglebug.com/3161
        return;
    #endif
    
        std::string executablePath = GetExecutablePath();
        EXPECT_NE("", executablePath);
    }
    
    // Test getting the executable directory
    TEST(SystemUtils, ExecutableDir)
    {
    #if defined(ANGLE_PLATFORM_FUCHSIA)
        // TODO: fuchsia support. http://anglebug.com/3161
        return;
    #endif
    
        std::string executableDir = GetExecutableDirectory();
        EXPECT_NE("", executableDir);
    
        std::string executablePath = GetExecutablePath();
        EXPECT_LT(executableDir.size(), executablePath.size());
        EXPECT_EQ(0, strncmp(executableDir.c_str(), executablePath.c_str(), executableDir.size()));
    }
    
    // Test setting environment variables
    TEST(SystemUtils, Environment)
    {
        constexpr char kEnvVarName[]  = "UNITTEST_ENV_VARIABLE";
        constexpr char kEnvVarValue[] = "The quick brown fox jumps over the lazy dog";
    
        bool setEnvDone = SetEnvironmentVar(kEnvVarName, kEnvVarValue);
        EXPECT_TRUE(setEnvDone);
    
        std::string readback = GetEnvironmentVar(kEnvVarName);
        EXPECT_EQ(kEnvVarValue, readback);
    
        bool unsetEnvDone = UnsetEnvironmentVar(kEnvVarName);
        EXPECT_TRUE(unsetEnvDone);
    
        readback = GetEnvironmentVar(kEnvVarName);
        EXPECT_EQ("", readback);
    }
    
    // Test running an external application and receiving its output
    TEST(SystemUtils, RunApp)
    {
    #if defined(ANGLE_PLATFORM_ANDROID)
        // TODO: android support. http://anglebug.com/3125
        return;
    #endif
    
    #if defined(ANGLE_PLATFORM_FUCHSIA)
        // TODO: fuchsia support. http://anglebug.com/3161
        return;
    #endif
    
        std::string executablePath = GetExecutableDirectory();
        EXPECT_NE(executablePath, "");
        executablePath += "/";
        executablePath += kRunAppHelperExecutable;
    
        std::vector<const char *> args = {executablePath.c_str(), kRunAppTestArg1, kRunAppTestArg2,
                                          nullptr};
    
        std::string stdoutOutput;
        std::string stderrOutput;
        int exitCode = EXIT_FAILURE;
    
        // Test that the application can be executed.
        bool ranApp = RunApp(args, &stdoutOutput, &stderrOutput, &exitCode);
        EXPECT_TRUE(ranApp);
        EXPECT_EQ(kRunAppTestStdout, NormalizeNewLines(stdoutOutput));
        EXPECT_EQ(kRunAppTestStderr, NormalizeNewLines(stderrOutput));
        EXPECT_EQ(EXIT_SUCCESS, exitCode);
    
        // Test that environment variables reach the cild.
        bool setEnvDone = SetEnvironmentVar(kRunAppTestEnvVarName, kRunAppTestEnvVarValue);
        EXPECT_TRUE(setEnvDone);
    
        ranApp = RunApp(args, &stdoutOutput, &stderrOutput, &exitCode);
        EXPECT_TRUE(ranApp);
        EXPECT_EQ("", stdoutOutput);
        EXPECT_EQ(kRunAppTestEnvVarValue, NormalizeNewLines(stderrOutput));
        EXPECT_EQ(EXIT_SUCCESS, exitCode);
    }
    
    }  // anonymous namespace