Shaka Packager SDK
file_test_util.cc
1 // Copyright 2022 Google LLC. All rights reserved.
2 //
3 // Use of this source code is governed by a BSD-style
4 // license that can be found in the LICENSE file or at
5 // https://developers.google.com/open-source/licenses/bsd
6 
7 #include <packager/file/file_test_util.h>
8 
9 #include <filesystem>
10 
11 namespace shaka {
12 
13 std::string generate_unique_temp_path() {
14  // Generate a unique name for a temporary file, using standard library
15  // routines, to avoid a circular dependency on any of our own code for
16  // generating temporary files. The template must end in 6 X's.
17  auto temp_path_template =
18  std::filesystem::temp_directory_path() / "packager-test.XXXXXX";
19  std::string temp_path_template_string = temp_path_template.string();
20 #if defined(OS_WIN)
21  // _mktemp will modify the string passed to it to reflect the generated name
22  // (replacing the X characters with something else).
23  _mktemp(temp_path_template_string.data());
24 #else
25  // mkstemp will create and open the file, modify the character points to
26  // reflect the generated name (replacing the X characters with something
27  // else), and return an open file descriptor. Then we close it and use the
28  // generated name.
29  int fd = mkstemp(temp_path_template_string.data());
30  close(fd);
31 #endif
32  return temp_path_template_string;
33 }
34 
35 void delete_file(const std::string& path) {
36  std::error_code ec;
37  std::filesystem::remove(std::filesystem::u8path(path), ec);
38  // Ignore errors.
39 }
40 
41 TempFile::TempFile() : path_(generate_unique_temp_path()) {}
42 
43 TempFile::~TempFile() {
44  std::error_code ec;
45  std::filesystem::remove(std::filesystem::u8path(path_), ec);
46  // Ignore errors.
47 }
48 
49 } // namespace shaka
All the methods that are virtual are virtual for mocking.
Definition: crypto_flags.cc:66