Skip to content

Commit a9f802d

Browse files
committed
use templfile instead of incrementing number
1 parent a202ecd commit a9f802d

File tree

3 files changed

+13
-30
lines changed

3 files changed

+13
-30
lines changed

llvm/utils/lit/lit/cl_arguments.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -178,12 +178,8 @@ def parse_args():
178178
execution_group.add_argument(
179179
"--use-unique-output-file-name",
180180
help="When enabled, lit will not overwrite existing test report files. "
181-
"Instead it will modify the file name until it finds a file name "
182-
"that does not already exist. An incrementing number starting from 1 "
183-
"will be added prior to the file extension, or for files without an "
184-
"extension, on the end of the fle name. For example, if 'results.xml' "
185-
"already exists, 'results.1.xml' will be used instead, if that exists, "
186-
"'results.2.xml' and so on. [Default: Off]",
181+
"Instead it will write to a new file named the same as the output file "
182+
"name but with a extra part before the file extension. [Default: Off]",
187183
action="store_true",
188184
)
189185
execution_group.add_argument(

llvm/utils/lit/lit/reports.py

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import itertools
55
import json
66
import os
7+
import tempfile
78

89
from xml.sax.saxutils import quoteattr as quo
910

@@ -24,19 +25,11 @@ def __init__(self, output_file):
2425

2526
def write_results(self, tests, elapsed):
2627
if self.use_unique_output_file_name:
27-
report_file = None
28-
filepath = self.output_file
29-
attempt = 0
30-
while report_file is None:
31-
try:
32-
report_file = open(filepath, "x")
33-
except FileExistsError:
34-
attempt += 1
35-
# If there is an extension insert before that because most
36-
# glob patterns for these will be '*.extension'. Otherwise
37-
# add to the end of the path.
38-
path, ext = os.path.splitext(self.output_file)
39-
filepath = path + f".{attempt}" + ext
28+
filename, ext = os.path.splitext(os.path.basename(self.output_file))
29+
fd, _ = tempfile.mkstemp(
30+
suffix=ext, prefix=f"{filename}.", dir=os.path.dirname(self.output_file)
31+
)
32+
report_file = os.fdopen(fd, "w")
4033
else:
4134
# Overwrite if the results already exist.
4235
report_file = open(self.output_file, "w")

llvm/utils/lit/tests/unique-output-file.py

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,21 +6,15 @@
66
# RUN: echo "test" > %t.xunit.xml
77
# RUN: not %{lit} --xunit-xml-output %t.xunit.xml %{inputs}/xunit-output
88
# RUN: FileCheck < %t.xunit.xml %s --check-prefix=NEW
9+
# NEW: <?xml version="1.0" encoding="UTF-8"?>
10+
# NEW-NEXT: <testsuites time="{{[0-9.]+}}">
11+
## (other tests will check the contents of the whole file)
912

1013
# RUN: rm -f %t.xunit*.xml
1114
# RUN: echo "test" > %t.xunit.xml
1215
## Files should not be overwritten with the option.
1316
# RUN: not %{lit} --xunit-xml-output %t.xunit.xml --use-unique-output-file-name %{inputs}/xunit-output
1417
# RUN: FileCheck < %t.xunit.xml %s --check-prefix=EXISTING
1518
# EXISTING: test
16-
## Results in a new file with "1" added.
17-
# RUN: FileCheck < %t.xunit.1.xml %s --check-prefix=NEW
18-
# NEW: <?xml version="1.0" encoding="UTF-8"?>
19-
# NEW-NEXT: <testsuites time="{{[0-9.]+}}">
20-
## (assuming that other tests check the whole contents of the file)
21-
22-
## The number should increment as many times as needed.
23-
# RUN: touch %t.xunit.2.xml %t.xunit.3.xml %t.xunit.4.xml
24-
25-
# RUN: not %{lit} --xunit-xml-output %t.xunit.xml --use-unique-output-file-name %{inputs}/xunit-output
26-
# RUN: FileCheck < %t.xunit.5.xml %s --check-prefix=NEW
19+
## Results in a new file with some discriminator added.
20+
# RUN: FileCheck < %t.xunit.*.xml %s --check-prefix=NEW

0 commit comments

Comments
 (0)