Skip to content

Commit f91469c

Browse files
Fix attachment order (#237)
* fix: keep timestamp fractional part * fix: suffix filenames with precise timestamp to aoid duplicates * test: fix additional tests * fix: pointer comparison is not required after all
1 parent 707250d commit f91469c

File tree

5 files changed

+41
-40
lines changed

5 files changed

+41
-40
lines changed

test/converters/converters_test.go

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,11 @@ import (
1111
"sort"
1212
"testing"
1313

14+
"github.com/bitrise-io/go-utils/log"
1415
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/test/converters/xcresult3"
1516
"github.com/bitrise-steplib/steps-deploy-to-bitrise-io/test/testreport"
1617
"github.com/google/go-cmp/cmp"
1718
"github.com/stretchr/testify/require"
18-
19-
"github.com/bitrise-io/go-utils/log"
2019
)
2120

2221
func TestXCresult3Converters(t *testing.T) {
@@ -64,11 +63,11 @@ func TestXCresult3Converters(t *testing.T) {
6463
Property: []testreport.Property{
6564
{
6665
Name: "attachment_0",
67-
Value: "Screenshot 2019-11-25 at 12.28.29 PM.jpeg",
66+
Value: "Screenshot 2019-11-25 at 12.28.29 PM_1574684909530999898.jpeg",
6867
},
6968
{
7069
Name: "attachment_1",
71-
Value: "Screenshot 2019-11-25 at 12.28.29 PM (1).jpeg",
70+
Value: "Screenshot 2019-11-25 at 12.28.29 PM_1574684909592000007.jpeg",
7271
},
7372
},
7473
},
@@ -78,7 +77,6 @@ func TestXCresult3Converters(t *testing.T) {
7877
ClassName: "_TtCC17rtgtrghtrgUITests17rtgtrghtrgUITests18rtgtrghtrg3UITests",
7978
Time: 0.036,
8079
},
81-
8280
// class rtgtrghtrg2UITests: XCTestCase
8381
{
8482
Name: "testExample()",
@@ -96,11 +94,11 @@ func TestXCresult3Converters(t *testing.T) {
9694
Property: []testreport.Property{
9795
{
9896
Name: "attachment_0",
99-
Value: "Screenshot 2019-11-25 at 12.28.29 PM (2).jpeg",
97+
Value: "Screenshot 2019-11-25 at 12.28.29 PM_1574684909736999988.jpeg",
10098
},
10199
{
102100
Name: "attachment_1",
103-
Value: "Screenshot 2019-11-25 at 12.28.29 PM (3).jpeg",
101+
Value: "Screenshot 2019-11-25 at 12.28.29 PM_1574684909776999950.jpeg",
104102
},
105103
},
106104
},
@@ -110,7 +108,6 @@ func TestXCresult3Converters(t *testing.T) {
110108
ClassName: "rtgtrghtrg2UITests",
111109
Time: 0.042,
112110
},
113-
114111
// class rtgtrghtrg4UITests: rtgtrghtrgUITests (so rtgtrghtrg4UITests inherits rtgtrghtrgUITests -> test cases merged and the base class name is rtgtrghtrg4UITests)
115112
{
116113
Name: "testExample()",
@@ -138,11 +135,11 @@ func TestXCresult3Converters(t *testing.T) {
138135
Property: []testreport.Property{
139136
{
140137
Name: "attachment_0",
141-
Value: "Screenshot 2019-11-25 at 12.28.30 PM.jpeg",
138+
Value: "Screenshot 2019-11-25 at 12.28.30 PM_1574684910020999908.jpeg",
142139
},
143140
{
144141
Name: "attachment_1",
145-
Value: "Screenshot 2019-11-25 at 12.28.30 PM (1).jpeg",
142+
Value: "Screenshot 2019-11-25 at 12.28.30 PM_1574684910062000036.jpeg",
146143
},
147144
},
148145
},
@@ -157,7 +154,6 @@ func TestXCresult3Converters(t *testing.T) {
157154
ClassName: "rtgtrghtrg4UITests",
158155
Time: 0.031,
159156
},
160-
161157
// class rtgtrghtrgUITests: XCTestCase
162158
{
163159
Name: "testExample()",
@@ -235,7 +231,7 @@ func TestXCresult3Converters(t *testing.T) {
235231
} else {
236232
require.NoError(t, err)
237233
}
238-
234+
239235
opts := []cmp.Option{
240236
cmp.Transformer("SortTestSuites", func(in []testreport.TestSuite) []testreport.TestSuite {
241237
s := append([]testreport.TestSuite{}, in...)
@@ -253,8 +249,8 @@ func TestXCresult3Converters(t *testing.T) {
253249
}),
254250
}
255251

256-
if !cmp.Equal(got, test.wantXML, opts...) {
257-
t.Fatalf("xml want: %+v, got: %+v", test.wantXML, got)
252+
if diff := cmp.Diff(test.wantXML, got, opts...); diff != "" {
253+
t.Fatalf("Test report mismatch (-want +got):\n%s", diff)
258254
}
259255
})
260256
}

test/converters/xcresult3/converter.go

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,6 @@ func extractAttachments(xcresultPath, outputPath string) (map[string][]string, e
282282
return nil, err
283283
}
284284

285-
var fileCounterMap = make(map[string]int)
286285
for _, attachmentDetail := range manifest {
287286
attachments := attachmentDetail.Attachments
288287

@@ -292,17 +291,8 @@ func extractAttachments(xcresultPath, outputPath string) (map[string][]string, e
292291

293292
for _, attachment := range attachments {
294293
oldPath := filepath.Join(outputPath, attachment.ExportedFileName)
295-
newPath := filepath.Join(outputPath, attachment.SuggestedHumanReadableName)
296-
297-
// If the file already exists, we need to rename it to avoid overwriting.
298-
if counter, exists := fileCounterMap[attachment.SuggestedHumanReadableName]; exists {
299-
fileExtensionWithDot := filepath.Ext(attachment.SuggestedHumanReadableName)
300-
fileNameWithoutExtension := strings.TrimSuffix(attachment.SuggestedHumanReadableName, fileExtensionWithDot)
301-
newPath = filepath.Join(outputPath, fmt.Sprintf("%s (%d)%s", fileNameWithoutExtension, counter, fileExtensionWithDot))
302-
fileCounterMap[attachment.SuggestedHumanReadableName] = counter + 1
303-
} else {
304-
fileCounterMap[attachment.SuggestedHumanReadableName] = 1
305-
}
294+
newFilename := createUniqueFilename(attachment)
295+
newPath := filepath.Join(outputPath, newFilename)
306296

307297
if err := os.Rename(oldPath, newPath); err != nil {
308298
// It is not a critical error if the rename fails because the file will be still exported just by its
@@ -326,6 +316,18 @@ func extractAttachments(xcresultPath, outputPath string) (map[string][]string, e
326316
return attachmentsMap, nil
327317
}
328318

319+
// Create unique filename using timestamp as suffix
320+
func createUniqueFilename(attachment model3.Attachment) string {
321+
timestamp := time.Time(attachment.Timestamp).UnixNano()
322+
323+
originalName := attachment.SuggestedHumanReadableName
324+
ext := filepath.Ext(originalName)
325+
nameWithoutExt := strings.TrimSuffix(originalName, ext)
326+
327+
// Format: originalname_timestamp.ext
328+
return fmt.Sprintf("%s_%d%s", nameWithoutExt, timestamp, ext)
329+
}
330+
329331
func stripTrailingParentheses(s string) string {
330332
return strings.TrimSuffix(s, "()")
331333
}

test/converters/xcresult3/converter_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,27 +93,27 @@ func TestConverter_XML(t *testing.T) {
9393
Property: []testreport.Property{
9494
{
9595
Name: "attachment_0",
96-
Value: "Screenshot 2022-02-10 at 02.57.39 PM.jpeg",
96+
Value: "Screenshot 2022-02-10 at 02.57.39 PM_1644505059194999933.jpeg",
9797
},
9898
{
9999
Name: "attachment_1",
100-
Value: "Screenshot 2022-02-10 at 02.57.39 PM (1).jpeg",
100+
Value: "Screenshot 2022-02-10 at 02.57.39 PM_1644505059388999938.jpeg",
101101
},
102102
{
103103
Name: "attachment_2",
104-
Value: "Screenshot 2022-02-10 at 02.57.44 PM.jpeg",
104+
Value: "Screenshot 2022-02-10 at 02.57.44 PM_1644505064670000076.jpeg",
105105
},
106106
{
107107
Name: "attachment_3",
108-
Value: "Screenshot 2022-02-10 at 02.57.47 PM.jpeg",
108+
Value: "Screenshot 2022-02-10 at 02.57.47 PM_1644505067144000053.jpeg",
109109
},
110110
{
111111
Name: "attachment_4",
112-
Value: "Screenshot 2022-02-10 at 02.57.47 PM (1).jpeg",
112+
Value: "Screenshot 2022-02-10 at 02.57.47 PM_1644505067476000070.jpeg",
113113
},
114114
{
115115
Name: "attachment_5",
116-
Value: "Screenshot 2022-02-10 at 02.57.47 PM (2).jpeg",
116+
Value: "Screenshot 2022-02-10 at 02.57.47 PM_1644505067992000102.jpeg",
117117
},
118118
},
119119
},
@@ -176,15 +176,15 @@ func TestConverter_XML(t *testing.T) {
176176
Property: []testreport.Property{
177177
{
178178
Name: "attachment_0",
179-
Value: "Screenshot 2021-02-09 at 08.35.51 AM.jpeg",
179+
Value: "Screenshot 2021-02-09 at 08.35.51 AM_1612859751989000082.jpeg",
180180
},
181181
{
182182
Name: "attachment_1",
183-
Value: "Screenshot 2021-02-09 at 08.35.52 AM.jpeg",
183+
Value: "Screenshot 2021-02-09 at 08.35.52 AM_1612859752052999973.jpeg",
184184
},
185185
{
186186
Name: "attachment_2",
187-
Value: "Screenshot 2021-02-09 at 08.35.52 AM (1).jpeg",
187+
Value: "Screenshot 2021-02-09 at 08.35.52 AM_1612859752052999973.jpeg",
188188
},
189189
},
190190
},

test/converters/xcresult3/model3/export.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@ func (t *Timestamp) UnmarshalJSON(b []byte) error {
2929
return err
3030
}
3131

32-
*t = Timestamp(time.Unix(int64(timestamp), 0))
32+
// Extract seconds and nanoseconds separately to preserve fractional part
33+
seconds := int64(timestamp)
34+
nanoseconds := int64((timestamp - float64(seconds)) * 1e9)
3335

36+
*t = Timestamp(time.Unix(seconds, nanoseconds))
3437
return nil
3538
}

test/testdata/ios_device_config_xml_output.golden

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
<testcase configuration-hash="" name="testLaunch()" classname="DarkAndLightModeUITestsLaunchTests" time="5">
1414
<failure></failure>
1515
<properties>
16-
<property name="attachment_0" value="Launch Screen_1_FD1B9BC4-364B-4191-88F9-48DD5B4B251F.png"></property>
17-
<property name="attachment_1" value="Launch Screen_1_1FDDCFCB-A490-4773-ABC1-96BC97459F6A.png"></property>
18-
<property name="attachment_2" value="Launch Screen_1_CEFDE93C-2C43-4D5B-80B5-0B27AD09CCC5.png"></property>
19-
<property name="attachment_3" value="Launch Screen_1_6CD9C2FF-7FFC-4C37-B82A-D208F800193B.png"></property>
16+
<property name="attachment_0" value="Launch Screen_1_FD1B9BC4-364B-4191-88F9-48DD5B4B251F_1702394166035000085.png"></property>
17+
<property name="attachment_1" value="Launch Screen_1_1FDDCFCB-A490-4773-ABC1-96BC97459F6A_1702394182316999912.png"></property>
18+
<property name="attachment_2" value="Launch Screen_1_CEFDE93C-2C43-4D5B-80B5-0B27AD09CCC5_1702394197773999929.png"></property>
19+
<property name="attachment_3" value="Launch Screen_1_6CD9C2FF-7FFC-4C37-B82A-D208F800193B_1702394211733000040.png"></property>
2020
</properties>
2121
</testcase>
2222
</testsuite>

0 commit comments

Comments
 (0)