aboutsummaryrefslogtreecommitdiff
path: root/test_util.h
blob: 7f923ed715621a7be26f61232667006955878a74 (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
/* test_util.h
 * Copyright 2021 The ChromiumOS Authors
 * Use of this source code is governed by a BSD-style license that can be
 * found in the LICENSE file.
 *
 * Utility functions in testing.
 */

#ifndef _TEST_UTIL_H_
#define _TEST_UTIL_H_

#include <stdio.h>

#include <memory>
#include <string>

#include "config_parser.h"

namespace mj {

namespace internal {

// Functor for |ScopedFILE| (below).
struct ScopedFILECloser {
  inline void operator()(FILE *x) const {
    if (x) {
      fclose(x);
    }
  }
};

// Functor for |ScopedConfigEntry| (below).
struct ScopedConfigEntryDeleter {
  inline void operator()(config_entry *entry) const {
    if (entry) {
      free(entry);
    }
  }
};

} // namespace internal

} // namespace mj

using ScopedFILE = std::unique_ptr<FILE, mj::internal::ScopedFILECloser>;
using ScopedConfigEntry =
    std::unique_ptr<config_entry, mj::internal::ScopedConfigEntryDeleter>;

/*
 * write_to_pipe: write a string as the file content into a pipe based
 * file handle. This is particularly useful when testing with temporary data
 * files, without dealing with complexities such as relative file path, file
 * permission and etc. However, a pipe has limited capacity so write_to_pipe
 * will hang when a big enough string is written. This is for use in testing
 * only.
 *
 * Returns a FILE* that contains @content.
 */

FILE *write_to_pipe(const std::string& content);

/*
 * source_path: return the path to a test fixture located in the current
 * source tree. This uses the `SRC` environment variable as the root of the
 * tree, falling back to the current directory.
 */
std::string source_path(const std::string& file);

#endif /* _TEST_UTIL_H_ */