aboutsummaryrefslogtreecommitdiff
path: root/test/test_main.cc
blob: b678ce9d7c4cf7e507d74073d8a0677e93585f2f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include <getopt.h>

#include <iostream>
#include <string>

#include "gtest/gtest.h"

// The test main must toggle logging and trace logging features because
// tests will be run in environments that support them (in which case we
// want as much debugging information as possible), and environments that
// don't, so they must be disabled. If ENABLE_PLATFORM_IMPL and
// ENABLE_TRACE_LOGGING are both false, then we should no Open Screen
// dependencies.
#ifdef ENABLE_PLATFORM_IMPL
#include "platform/impl/logging.h"
#endif
#ifdef ENABLE_TRACE_LOGGING
#include "platform/impl/text_trace_logging_platform.h"
#endif

namespace {
void LogUsage(const char* argv0) {
  std::cerr << R"(
usage: )" << argv0
            << R"( <options>

options:
  -t, --tracing: Enable performance tracing logging.

  -v, --verbose: Enable verbose logging.

  -h, --help: Show this help message.
)";
}

struct GlobalTestState {
#ifdef ENABLE_TRACE_LOGGING
  std::unique_ptr<openscreen::TextTraceLoggingPlatform> trace_logger;
#endif
  bool args_are_valid = false;
};

GlobalTestState InitFromArgs(int argc, char** argv) {
  // A note about modifying command line arguments: consider uniformity
  // between all Open Screen executables. If it is a platform feature
  // being exposed, consider if it applies to the standalone receiver,
  // standalone sender, osp demo, and test_main argument options.
  const struct option kArgumentOptions[] = {
      {"tracing", no_argument, nullptr, 't'},
      {"verbose", no_argument, nullptr, 'v'},
      {"help", no_argument, nullptr, 'h'},
      {nullptr, 0, nullptr, 0}};

  GlobalTestState state;
  int ch = -1;
  while ((ch = getopt_long(argc, argv, "tvh", kArgumentOptions, nullptr)) !=
         -1) {
    switch (ch) {
#ifdef ENABLE_TRACE_LOGGING
      case 't':
        state.trace_logger =
            std::make_unique<openscreen::TextTraceLoggingPlatform>();
        break;
#endif
// When not built with Chrome, log level default is warning. When we are built
// with Chrome, we have no way of knowing or setting log level.
#ifdef ENABLE_PLATFORM_IMPL
      case 'v':
        openscreen::SetLogLevel(openscreen::LogLevel::kVerbose);
        break;
#endif
      case 'h':
        LogUsage(argv[0]);
        return state;
    }
  }

  state.args_are_valid = true;
  return state;
}
}  // namespace

// Googletest strongly recommends that we roll our own main
// function if we want to do global test environment setup.
// See the below link for more info;
// https://github.com/google/googletest/blob/master/docs/advanced.md#sharing-resources-between-tests-in-the-same-test-suite
// TODO(issuetracker.google.com/172242670): rename reference to "main"
// once googletest has a "main" branch.
// This main method is a drop-in replacement for anywhere that currently
// depends on gtest_main, meaning it can be linked into any test-only binary
// to provide a main implementation that supports setting flags and other
// state that we want shared between all tests.
int main(int argc, char** argv) {
  auto state = InitFromArgs(argc, argv);
  if (!state.args_are_valid) {
    return 1;
  }

  testing::InitGoogleTest(&argc, argv);
  return RUN_ALL_TESTS();
}