Skip to content

Commit eeb4f4d

Browse files
authored
Merge pull request #2998 from Freed-Wu/pre-commit
Exit 1 when check error
2 parents cdb1b09 + 9f25c27 commit eeb4f4d

File tree

3 files changed

+206
-192
lines changed

3 files changed

+206
-192
lines changed

script/cli/check.lua

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -5,97 +5,104 @@ local json = require 'json'
55
local jsonb = require 'json-beautify'
66
local util = require 'utility'
77

8+
local export = {}
89

9-
local numThreads = tonumber(NUM_THREADS or 1)
10+
function export.runCLI()
11+
local numThreads = tonumber(NUM_THREADS or 1)
1012

11-
local exe
12-
local minIndex = -1
13-
while arg[minIndex] do
14-
exe = arg[minIndex]
15-
minIndex = minIndex - 1
16-
end
17-
-- TODO: is this necessary? got it from the shell.lua helper in bee.lua tests
18-
if platform.os == 'windows' and not exe:match('%.[eE][xX][eE]$') then
19-
exe = exe..'.exe'
20-
end
13+
local exe
14+
local minIndex = -1
15+
while arg[minIndex] do
16+
exe = arg[minIndex]
17+
minIndex = minIndex - 1
18+
end
19+
-- TODO: is this necessary? got it from the shell.lua helper in bee.lua tests
20+
if platform.os == 'windows' and not exe:match('%.[eE][xX][eE]$') then
21+
exe = exe..'.exe'
22+
end
2123

22-
local function logFileForThread(threadId)
23-
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
24-
end
24+
local function logFileForThread(threadId)
25+
return LOGPATH .. '/check-partial-' .. threadId .. '.json'
26+
end
2527

26-
local function buildArgs(threadId)
27-
local args = {exe}
28-
local skipNext = false
29-
for i = 1, #arg do
30-
local arg = arg[i]
31-
-- --check needs to be transformed into --check_worker
32-
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
33-
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
34-
-- --check_out_path needs to be removed if we have more than one thread
35-
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
36-
if not arg:match('%-%-%w*=') then
37-
skipNext = true
38-
end
39-
else
40-
if skipNext then
41-
skipNext = false
28+
local function buildArgs(threadId)
29+
local args = {exe}
30+
local skipNext = false
31+
for i = 1, #arg do
32+
local arg = arg[i]
33+
-- --check needs to be transformed into --check_worker
34+
if arg:lower():match('^%-%-check$') or arg:lower():match('^%-%-check=') then
35+
args[#args + 1] = arg:gsub('%-%-%w*', '--check_worker')
36+
-- --check_out_path needs to be removed if we have more than one thread
37+
elseif arg:lower():match('%-%-check_out_path') and numThreads > 1 then
38+
if not arg:match('%-%-%w*=') then
39+
skipNext = true
40+
end
4241
else
43-
args[#args + 1] = arg
42+
if skipNext then
43+
skipNext = false
44+
else
45+
args[#args + 1] = arg
46+
end
4447
end
4548
end
49+
args[#args + 1] = '--thread_id'
50+
args[#args + 1] = tostring(threadId)
51+
if numThreads > 1 then
52+
args[#args + 1] = '--quiet'
53+
args[#args + 1] = '--check_out_path'
54+
args[#args + 1] = logFileForThread(threadId)
55+
end
56+
return args
4657
end
47-
args[#args + 1] = '--thread_id'
48-
args[#args + 1] = tostring(threadId)
58+
4959
if numThreads > 1 then
50-
args[#args + 1] = '--quiet'
51-
args[#args + 1] = '--check_out_path'
52-
args[#args + 1] = logFileForThread(threadId)
60+
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
5361
end
54-
return args
55-
end
5662

57-
if numThreads > 1 then
58-
print(lang.script('CLI_CHECK_MULTIPLE_WORKERS', numThreads))
59-
end
60-
61-
local procs = {}
62-
for i = 1, numThreads do
63-
local process, err = subprocess.spawn({buildArgs(i)})
64-
if err then
65-
print(err)
66-
end
67-
if process then
68-
procs[#procs + 1] = process
63+
local procs = {}
64+
for i = 1, numThreads do
65+
local process, err = subprocess.spawn({buildArgs(i)})
66+
if err then
67+
print(err)
68+
end
69+
if process then
70+
procs[#procs + 1] = process
71+
end
6972
end
70-
end
7173

72-
for _, process in ipairs(procs) do
73-
process:wait()
74-
end
74+
local checkPassed = true
75+
for _, process in ipairs(procs) do
76+
checkPassed = process:wait() == 0 and checkPassed
77+
end
7578

76-
local outpath = CHECK_OUT_PATH
77-
if outpath == nil then
78-
outpath = LOGPATH .. '/check.json'
79-
end
79+
local outpath = CHECK_OUT_PATH
80+
if outpath == nil then
81+
outpath = LOGPATH .. '/check.json'
82+
end
8083

81-
if numThreads > 1 then
82-
local mergedResults = {}
83-
local count = 0
84-
for i = 1, numThreads do
85-
local result = json.decode(util.loadFile(logFileForThread(i)) or '[]')
86-
for k, v in pairs(result) do
87-
local entries = mergedResults[k] or {}
88-
mergedResults[k] = entries
89-
for _, entry in ipairs(v) do
90-
entries[#entries + 1] = entry
91-
count = count + 1
84+
if numThreads > 1 then
85+
local mergedResults = {}
86+
local count = 0
87+
for i = 1, numThreads do
88+
local result = json.decode(util.loadFile(logFileForThread(i)) or '[]')
89+
for k, v in pairs(result) do
90+
local entries = mergedResults[k] or {}
91+
mergedResults[k] = entries
92+
for _, entry in ipairs(v) do
93+
entries[#entries + 1] = entry
94+
count = count + 1
95+
end
9296
end
9397
end
98+
util.saveFile(outpath, jsonb.beautify(mergedResults))
99+
if count == 0 then
100+
print(lang.script('CLI_CHECK_SUCCESS'))
101+
else
102+
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
103+
end
94104
end
95-
util.saveFile(outpath, jsonb.beautify(mergedResults))
96-
if count == 0 then
97-
print(lang.script('CLI_CHECK_SUCCESS'))
98-
else
99-
print(lang.script('CLI_CHECK_RESULTS', count, outpath))
100-
end
105+
return checkPassed and 0 or 1
101106
end
107+
108+
return export

0 commit comments

Comments
 (0)