feat: port media/test (#1116)

Rewrite test_data_util.cc to locate files relative to the source file
itself, rather than using a service from chromium `base::`.

Issue #1047 (CMake porting)
Issue #346 (absl porting)
This commit is contained in:
Joey Parrish 2022-10-25 09:15:54 -07:00 committed by GitHub
parent 31ad9a2539
commit a9a4f0d52c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 48 additions and 94 deletions

View File

@ -41,6 +41,7 @@ endif()
# Subdirectories with their own CMakeLists.txt, all of whose targets are built.
add_subdirectory(file)
add_subdirectory(kv_pairs)
add_subdirectory(media)
add_subdirectory(status)
add_subdirectory(third_party)
add_subdirectory(tools)

View File

@ -0,0 +1,8 @@
# Copyright 2022 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
# Subdirectories with their own CMakeLists.txt, all of whose targets are built.
add_subdirectory(test)

View File

@ -0,0 +1,9 @@
# Copyright 2022 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
add_library(test_data_util STATIC
test_data_util.cc)
target_link_libraries(test_data_util glog)

View File

@ -1,35 +0,0 @@
# Copyright 2014 Google LLC. All rights reserved.
#
# Use of this source code is governed by a BSD-style
# license that can be found in the LICENSE file or at
# https://developers.google.com/open-source/licenses/bsd
{
'variables': {
'shaka_code': 1,
},
'targets': [
{
'target_name': 'run_tests_with_atexit_manager',
'type': '<(component)',
'sources': [
'run_tests_with_atexit_manager.cc',
],
'dependencies': [
'../../testing/gtest.gyp:gtest',
],
},
{
'target_name': 'media_test_support',
'type': '<(component)',
'sources': [
'test_data_util.cc',
'test_data_util.h',
],
'dependencies': [
'../../base/base.gyp:base',
'run_tests_with_atexit_manager',
],
},
],
}

View File

@ -1,29 +0,0 @@
// Copyright 2014 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#include <gtest/gtest.h>
#include "packager/base/at_exit.h"
#include "packager/base/command_line.h"
#include "packager/base/files/file_path.h"
#include "packager/base/logging.h"
#include "packager/base/path_service.h"
int main(int argc, char **argv) {
base::AtExitManager exit;
// Needed to enable VLOG/DVLOG through --vmodule or --v.
base::CommandLine::Init(argc, argv);
// Set up logging.
logging::LoggingSettings log_settings;
log_settings.logging_dest = logging::LOG_TO_SYSTEM_DEBUG_LOG;
CHECK(logging::InitLogging(log_settings));
::testing::InitGoogleTest(&argc, argv);
return RUN_ALL_TESTS();
}

View File

@ -4,41 +4,41 @@
#include "packager/media/test/test_data_util.h"
#include "packager/base/files/file_util.h"
#include "packager/base/logging.h"
#include "packager/base/path_service.h"
#include "glog/logging.h"
namespace shaka {
namespace media {
base::FilePath GetTestDataFilePath(const std::string& name) {
base::FilePath file_path;
CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));
file_path = file_path.Append(FILE_PATH_LITERAL("packager"))
.Append(FILE_PATH_LITERAL("media"))
.Append(FILE_PATH_LITERAL("test"))
.Append(FILE_PATH_LITERAL("data"))
.AppendASCII(name);
return file_path;
// Returns a file path for a file in the media/test/data directory.
std::filesystem::path GetTestDataFilePath(const std::string& name) {
std::filesystem::path header_path(__FILE__);
return header_path.parent_path() / "data" / name;
}
base::FilePath GetAppTestDataFilePath(const std::string& name) {
base::FilePath file_path;
CHECK(PathService::Get(base::DIR_SOURCE_ROOT, &file_path));
file_path = file_path.Append(FILE_PATH_LITERAL("packager"))
.Append(FILE_PATH_LITERAL("app"))
.Append(FILE_PATH_LITERAL("test"))
.Append(FILE_PATH_LITERAL("testdata"))
.AppendASCII(name);
return file_path;
// Returns a file path for a file in the media/app/test/testdata directory.
std::filesystem::path GetAppTestDataFilePath(const std::string& name) {
std::filesystem::path header_path(__FILE__);
return header_path.parent_path().parent_path() / "app" / "test" / "testdata" /
name;
}
// Reads a test file from media/test/data directory and returns its content.
std::vector<uint8_t> ReadTestDataFile(const std::string& name) {
std::string buffer;
CHECK(base::ReadFileToString(GetTestDataFilePath(name), &buffer));
return std::vector<uint8_t>(buffer.begin(), buffer.end());
std::filesystem::path path = GetTestDataFilePath(name);
FILE* f = fopen(path.string().c_str(), "rb");
if (!f) {
LOG(FATAL) << "Failed to read test data from " << path;
return std::vector<uint8_t>();
}
std::vector<uint8_t> data;
data.resize(std::filesystem::file_size(path));
size_t size = fread(data.data(), 1, data.size(), f);
data.resize(size);
fclose(f);
return data;
}
} // namespace media

View File

@ -7,18 +7,18 @@
#include <stdint.h>
#include <filesystem>
#include <string>
#include "packager/base/files/file_path.h"
#include <vector>
namespace shaka {
namespace media {
// Returns a file path for a file in the media/test/data directory.
base::FilePath GetTestDataFilePath(const std::string& name);
std::filesystem::path GetTestDataFilePath(const std::string& name);
// Returns a file path for a file in the media/app/test/testdata directory.
base::FilePath GetAppTestDataFilePath(const std::string& name);
std::filesystem::path GetAppTestDataFilePath(const std::string& name);
// Reads a test file from media/test/data directory and returns its content.
std::vector<uint8_t> ReadTestDataFile(const std::string& name);