File: MakefileDepsParserTest.cpp

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (177 lines) | stat: -rw-r--r-- 6,187 bytes parent folder | download
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
//===- unittests/Core/MakefileDepsParserTest.cpp --------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//

#include "llbuild/Core/MakefileDepsParser.h"

#include "gtest/gtest.h"

#include <string>
#include <vector>

using namespace llbuild;
using namespace llbuild::core;

namespace {

TEST(MakefileDepsParserTest, basic) {
  typedef std::pair<std::string, std::vector<std::string>> RuleRecord;
  typedef std::pair<std::string, uint64_t> ErrorRecord;
  struct Testactions : public MakefileDepsParser::ParseActions {
    std::vector<RuleRecord> records;
    std::vector<std::pair<std::string, uint64_t>> errors;

    virtual void error(StringRef message, uint64_t position) override {
      errors.emplace_back(message.str(), position);
    }

    virtual void actOnRuleStart(StringRef name,
                                StringRef unescapedWord) override {
      records.emplace_back(unescapedWord.str(),
                           std::vector<std::string>{});
    }

    virtual void actOnRuleDependency(StringRef dependency,
                                     StringRef unescapedWord) override {
      assert(!records.empty());
      records.back().second.push_back(unescapedWord.str());
    }
    virtual void actOnRuleEnd() override {}
  };

  Testactions actions;
  std::string input;

  // Check a basic valid input with an escape sequence.
  input = "a: b\\$c d\\\ne";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b\\$c", "d", "e" }),
            actions.records[0]);

  // Check a basic valid input.
  actions.errors.clear();
  actions.records.clear();
  input = "a: b c d";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b", "c", "d" }),
            actions.records[0]);

  // Check a valid input with various escaped spaces.
  actions.errors.clear();
  actions.records.clear();
  input = "a\\ b: a\\ b a$$b a\\b a\\#b a\\\\\\ b";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a b", {
        "a b", "a$b", "a\\b", "a#b", "a\\ b" }),
    actions.records[0]);

  // Check a basic valid input with two rules.
  actions.errors.clear();
  actions.records.clear();
  input = "a: b c d\none: two three";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(2U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b", "c", "d" }),
            actions.records[0]);
  EXPECT_EQ(RuleRecord("one", { "two", "three" }),
            actions.records[1]);

  // Check a basic valid input with two rules when ignoring subsequent outputs.
  actions.errors.clear();
  actions.records.clear();
  input = "a: b c d\none: two three";
  MakefileDepsParser(StringRef(input), actions, true).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b", "c", "d" }),
            actions.records[0]);

  // Check a valid input with a trailing newline.
  input = "out: \\\n  in1\n";
  actions.errors.clear();
  actions.records.clear();
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("out", { "in1" }),
            actions.records[0]);

  input = "out: \\\r\n  in1\n";
  actions.errors.clear();
  actions.records.clear();
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("out", { "in1" }),
            actions.records[0]);

  // Check error case if leading garbage.
  actions.errors.clear();
  actions.records.clear();
  input = "  $ a";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(1U, actions.errors.size());
  EXPECT_EQ(actions.errors[0],
            ErrorRecord("unexpected character in file", 2U));
  EXPECT_EQ(0U, actions.records.size());

  // Check error case if no ':'.
  actions.errors.clear();
  actions.records.clear();
  input = "a";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(1U, actions.errors.size());
  EXPECT_EQ(actions.errors[0],
            ErrorRecord("missing ':' following rule", 1U));
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", {}),
            actions.records[0]);


  // Check error case in dependency list.
  actions.errors.clear();
  actions.records.clear();
  input = "a: b$";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(1U, actions.errors.size());
  EXPECT_EQ(actions.errors[0],
            ErrorRecord("unexpected character in prerequisites", 4U));
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b" }),
            actions.records[0]);

  // Check that we can parse filenames with special characters.
  actions.errors.clear();
  actions.records.clear();
  input = "/=>\\ ;|%.o : /=>\\ ;|%.swift";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("/=> ;|%.o", { "/=> ;|%.swift" }), actions.records[0]);
  
  // Check that we can parse filenames with colons.
  actions.errors.clear();
  actions.records.clear();
  input = "a: b:c";
  MakefileDepsParser(StringRef(input), actions, false).parse();
  EXPECT_EQ(0U, actions.errors.size());
  EXPECT_EQ(1U, actions.records.size());
  EXPECT_EQ(RuleRecord("a", { "b:c" }), actions.records[0]);
}

}