-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest.update_features.ex
More file actions
236 lines (188 loc) · 8 KB
/
test.update_features.ex
File metadata and controls
236 lines (188 loc) · 8 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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
defmodule Mix.Tasks.Test.UpdateFeatures do
@shortdoc "Updates test files with @tag required_features: tags based on XML content"
@moduledoc """
Updates all SCION and W3C test files with @tag required_features: tags.
Analyzes the XML content in each test file using Statifier.FeatureDetector to determine
which SCXML features are required, then adds or updates the @tag required_features:
tag accordingly.
## Usage
mix test.update_features
## Options
--dry-run Show what would be changed without modifying files
--verbose Show detailed output for each file processed
"""
use Mix.Task
alias Statifier.FeatureDetector
@switches [dry_run: :boolean, verbose: :boolean]
@aliases [d: :dry_run, v: :verbose]
@spec run([String.t()]) :: no_return()
def run(args) do
{opts, _args} = OptionParser.parse!(args, strict: @switches, aliases: @aliases)
dry_run = Keyword.get(opts, :dry_run, false)
verbose = Keyword.get(opts, :verbose, false)
Mix.shell().info("🔍 Analyzing test files and updating @tag required_features: tags...")
if dry_run, do: Mix.shell().info(" (DRY RUN MODE - No files will be modified)")
# Process SCION test files
scion_files = find_test_files("test/scion_tests/**/*.exs")
w3c_files = find_test_files("test/scxml_tests/**/*.exs")
scion_results = process_test_files(scion_files, :scion, dry_run, verbose)
w3c_results = process_test_files(w3c_files, :w3c, dry_run, verbose)
# Report results
total_processed = scion_results.processed + w3c_results.processed
total_updated = scion_results.updated + w3c_results.updated
total_errors = scion_results.errors + w3c_results.errors
Mix.shell().info("\n📊 Summary:")
Mix.shell().info(
" SCION tests: #{scion_results.processed} processed, #{scion_results.updated} updated, #{scion_results.errors} errors"
)
Mix.shell().info(
" W3C tests: #{w3c_results.processed} processed, #{w3c_results.updated} updated, #{w3c_results.errors} errors"
)
Mix.shell().info(
" TOTAL: #{total_processed} processed, #{total_updated} updated, #{total_errors} errors"
)
if total_errors > 0 do
Mix.shell().error("\n❌ #{total_errors} files had errors during processing.")
System.halt(1)
else
Mix.shell().info("\n✅ Successfully processed all test files!")
end
end
defp find_test_files(pattern) do
Path.wildcard(pattern)
|> Enum.filter(&File.exists?/1)
|> Enum.sort()
end
defp process_test_files(files, suite_type, dry_run, verbose) do
suite_name = if suite_type == :scion, do: "SCION", else: "W3C"
results = %{processed: 0, updated: 0, errors: 0}
Enum.reduce(files, results, fn file_path, acc ->
case process_single_test_file(file_path, suite_type, dry_run, verbose) do
result ->
handle_processing_result(result, verbose, suite_name, file_path, acc)
end
end)
end
defp handle_processing_result(result, verbose, suite_name, file_path, acc) do
case result do
{:updated, _features} ->
if verbose do
Mix.shell().info(" ✅ #{suite_name}: Updated #{Path.basename(file_path)}")
end
%{acc | processed: acc.processed + 1, updated: acc.updated + 1}
{:unchanged, _features} ->
if verbose do
Mix.shell().info(
" ⏭️ #{suite_name}: No changes needed for #{Path.basename(file_path)}"
)
end
%{acc | processed: acc.processed + 1}
{:error, reason} ->
Mix.shell().error(
" ❌ #{suite_name}: Error processing #{Path.basename(file_path)}: #{reason}"
)
%{acc | processed: acc.processed + 1, errors: acc.errors + 1}
end
end
defp process_single_test_file(file_path, suite_type, dry_run, _verbose) do
content = File.read!(file_path)
with xml when not is_nil(xml) <- extract_xml_from_test(content),
%MapSet{} = features <- FeatureDetector.detect_features(xml) do
features_list = MapSet.to_list(features) |> Enum.sort()
process_features_update(content, features_list, suite_type, file_path, dry_run)
else
nil -> {:error, "No XML content found in test"}
_error -> {:error, "Feature detection failed"}
end
rescue
exception ->
{:error, Exception.message(exception)}
end
defp process_features_update(content, features_list, suite_type, file_path, dry_run) do
case update_required_features_tag(content, features_list, suite_type) do
{:updated, new_content} ->
unless dry_run do
File.write!(file_path, new_content)
end
{:updated, features_list}
{:unchanged, _content} ->
{:unchanged, features_list}
end
end
defp extract_xml_from_test(content) do
# Look for xml = """ ... """ pattern in test files
case Regex.run(~r/xml\s*=\s*"""\s*(.+?)\s*"""/ms, content, capture: :all_but_first) do
[xml_content] ->
# Clean up any leading/trailing whitespace and ensure proper XML format
xml_content
|> String.trim()
nil ->
# Try alternative pattern: looking for <scxml> directly in triple quotes
case Regex.run(~r/"""\s*(<\?xml.+?<\/scxml>|<scxml.+?<\/scxml>)/ms, content,
capture: :all_but_first
) do
[xml_content] -> String.trim(xml_content)
nil -> nil
end
end
end
defp update_required_features_tag(content, features_list, suite_type) do
# Create the new @tag required_features: line
new_tag =
if features_list == [] do
" @tag required_features: []"
else
features_formatted = Enum.map_join(features_list, ", ", &inspect/1)
single_line_tag = " @tag required_features: [#{features_formatted}]"
# Use multiline format if the single line would be too long (>120 chars)
if String.length(single_line_tag) > 120 do
features_multiline = Enum.map_join(features_list, ",\n ", &inspect/1)
" @tag required_features: [\n #{features_multiline}\n ]"
else
single_line_tag
end
end
# Check if @required_features or @tag required_features: already exists
# Use multiline matching to handle multiline @tag required_features: blocks properly
case Regex.run(
~r/^(\s*)@(required_features\s+\[[^\]]*(?:\n[^\]]*)*\]|tag required_features:\s*\[[^\]]*(?:\n[^\]]*)*\])/m,
content
) do
[existing_line, _indent, _match_group] ->
# Replace existing @required_features or @tag required_features:
current_tag = String.trim_leading(existing_line)
if current_tag == String.trim_leading(new_tag) do
{:unchanged, content}
else
new_content = String.replace(content, existing_line, new_tag)
{:updated, new_content}
end
nil ->
# Add new @tag required_features: after existing @tag lines
case add_required_features_tag(content, new_tag, suite_type) do
^content -> {:unchanged, content}
new_content -> {:updated, new_content}
end
end
end
defp add_required_features_tag(content, new_tag, suite_type) do
suite_tag = if suite_type == :scion, do: "@tag :scion", else: "@tag :scxml_w3"
# Find the position after the last @tag line to insert @required_features
lines = String.split(content, "\n")
{new_lines, _inserted_flag} =
Enum.reduce(lines, {[], false}, fn line, {acc_lines, inserted} ->
cond do
# If we haven't inserted yet and this line contains the suite tag
not inserted and String.contains?(line, suite_tag) ->
{acc_lines ++ [line, new_tag], true}
# If we haven't inserted yet and we see a line that's not a tag (like test definition)
not inserted and String.match?(line, ~r/^\s*(test\s|def\s)/) ->
# Insert before this line
{acc_lines ++ [new_tag, line], true}
true ->
{acc_lines ++ [line], inserted}
end
end)
Enum.join(new_lines, "\n")
end
end