-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathTestsFlow.cpp
More file actions
163 lines (134 loc) · 3.19 KB
/
Copy pathTestsFlow.cpp
File metadata and controls
163 lines (134 loc) · 3.19 KB
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
/**
*
* @file TestsFlow.cpp
* @author Gaspard Kirira
*
* Copyright 2025, Gaspard Kirira. All rights reserved.
* https://github.com/vixcpp/vix
* Use of this source code is governed by a MIT license
* that can be found in the License file.
*
* Vix.cpp
*
*/
#include <vix/cli/commands/tests/TestsDetail.hpp>
#include <filesystem>
#include <string>
#include <vector>
#include <system_error>
namespace vix::commands::TestsCommand::detail
{
namespace fs = std::filesystem;
static bool looks_like_path(const std::string &s)
{
if (s.empty())
return false;
if (s[0] == '-')
return false;
if (s.find('/') != std::string::npos)
return true;
if (s.rfind("./", 0) == 0 || s.rfind("../", 0) == 0)
return true;
std::error_code ec;
return fs::exists(fs::path(s), ec);
}
static fs::path normalize_project_dir(const fs::path &p)
{
std::error_code ec;
fs::path abs = fs::absolute(p, ec);
if (ec)
abs = p;
fs::path canon = fs::weakly_canonical(abs, ec);
if (ec)
return abs;
return canon;
}
Options parse(const std::vector<std::string> &args)
{
Options opt{};
opt.forwarded.reserve(args.size() + 8);
opt.ctestArgs.reserve(16);
std::vector<std::string> left;
left.reserve(args.size());
bool afterSep = false;
for (const auto &a : args)
{
if (!afterSep && a == "--")
{
afterSep = true;
continue;
}
if (afterSep)
opt.ctestArgs.push_back(a);
else
left.push_back(a);
}
bool projectSet = false;
for (std::size_t i = 0; i < left.size(); ++i)
{
const std::string &a = left[i];
if (a == "--watch")
{
opt.watch = true;
continue;
}
if (a == "--list")
{
opt.list = true;
continue;
}
if (a == "--fail-fast")
{
opt.failFast = true;
continue;
}
if (a == "--run")
{
opt.runAfter = true;
continue;
}
if (a == "--raw")
{
opt.raw = true;
continue;
}
if (a == "--test" || a == "-R")
{
if (i + 1 < left.size())
{
opt.testPattern = left[++i];
continue;
}
opt.forwarded.push_back(a);
continue;
}
constexpr const char testPrefix[] = "--test=";
if (a.rfind(testPrefix, 0) == 0)
{
opt.testPattern = a.substr(sizeof(testPrefix) - 1);
continue;
}
if (!projectSet && looks_like_path(a))
{
opt.projectDir = normalize_project_dir(fs::path(a));
projectSet = true;
continue;
}
opt.forwarded.push_back(a);
}
if (!projectSet)
opt.projectDir = normalize_project_dir(fs::current_path());
if (opt.list)
opt.ctestArgs.insert(opt.ctestArgs.begin(), "--show-only");
if (opt.failFast)
opt.ctestArgs.push_back("--stop-on-failure");
if (!opt.testPattern.empty())
{
opt.ctestArgs.push_back("-R");
opt.ctestArgs.push_back(opt.testPattern);
}
if (opt.runAfter)
opt.forwarded.push_back("--run");
return opt;
}
} // namespace vix::commands::TestsCommand::detail