Skip to content

Commit 5fb6706

Browse files
author
charlierudolph
committed
attachments: remove intermediate conversion to string
1 parent 561f9a8 commit 5fb6706

File tree

3 files changed

+40
-35
lines changed

3 files changed

+40
-35
lines changed

features/attachments.feature

+14-22
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ Feature: Attachments
1919
"""
2020
var hooks = function () {
2121
this.Before(function(scenario, callback) {
22-
scenario.attach(new Buffer([100, 97, 116, 97]), 'image/png');
22+
scenario.attach(new Buffer([137, 80, 78, 71]), 'image/png');
2323
callback();
2424
});
2525
};
@@ -60,7 +60,7 @@ Feature: Attachments
6060
"embeddings": [
6161
{
6262
"mime_type": "image/png",
63-
"data": "ZGF0YQ=="
63+
"data": "iVBORw=="
6464
}
6565
]
6666
},
@@ -101,27 +101,19 @@ Feature: Attachments
101101
"""
102102
And a file named "features/support/hooks.js" with:
103103
"""
104+
var Stream = require('stream');
105+
104106
var hooks = function () {
105107
this.Before(function(scenario, callback) {
106-
var Stream = require('stream');
107-
var versionParts = /v(\d+)\.(\d+)\.(\d+)/.exec(process.version);
108-
var major = parseInt(versionParts[0], 10);
109-
var minor = parseInt(versionParts[1], 10);
110-
111-
if (major > 0 || minor >= 10) {
112-
var stream = new Stream.Readable();
113-
stream._read = function() {};
114-
stream.push(new Buffer([100, 97, 116, 97]));
115-
stream.push(null);
108+
var stream = new Stream.Readable();
109+
stream._read = function() {};
110+
stream.push(new Buffer([137, 80]));
111+
stream.push(new Buffer([78, 71]));
112+
stream.push(null);
116113
117-
scenario.attach(stream, 'image/png', function(error) {
118-
callback(error);
119-
});
120-
}
121-
else {
122-
scenario.attach(new Buffer([100, 97, 116, 97]), 'image/png');
123-
callback();
124-
}
114+
scenario.attach(stream, 'image/png', function(error) {
115+
callback(error);
116+
});
125117
});
126118
};
127119
@@ -156,12 +148,12 @@ Feature: Attachments
156148
},
157149
"arguments": [],
158150
"match": {
159-
"location": "<current-directory>/features/support/hooks.js:2"
151+
"location": "<current-directory>/features/support/hooks.js:4"
160152
},
161153
"embeddings": [
162154
{
163155
"mime_type": "image/png",
164-
"data": "ZGF0YQ=="
156+
"data": "iVBORw=="
165157
}
166158
]
167159
},

lib/cucumber/api/scenario.js

+10-11
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ function Scenario(astScenario, scenarioResult) {
77
return value && typeof value === 'object' && typeof value.pipe === 'function';
88
}
99

10-
function attachString(string, mimeType) {
11-
var attachment = Cucumber.Runtime.Attachment({mimeType: mimeType, data: string});
10+
function attachData(data, mimeType) {
11+
var attachment = Cucumber.Runtime.Attachment({mimeType: mimeType, data: data});
1212
attachments.push(attachment);
1313
}
1414

@@ -19,16 +19,11 @@ function Scenario(astScenario, scenarioResult) {
1919
buffers.push(chunk);
2020
});
2121
stream.on('end', function () {
22-
attachString(Buffer.concat(buffers).toString(), mimeType);
22+
attachData(Buffer.concat(buffers), mimeType);
2323
callback();
2424
});
2525
}
2626

27-
function attachBuffer(buffer, mimeType, callback) {
28-
attachString(buffer.toString(), mimeType);
29-
if (callback) callback();
30-
}
31-
3227
var self = {
3328
getKeyword: function getKeyword() { return astScenario.getKeyword(); },
3429
getName: function getName() { return astScenario.getName(); },
@@ -58,14 +53,17 @@ function Scenario(astScenario, scenarioResult) {
5853
if (!mimeType)
5954
throw Error(Scenario.ATTACH_MISSING_MIME_TYPE_ARGUMENT);
6055

61-
attachBuffer(data, mimeType, callback);
56+
attachData(data, mimeType);
57+
if (callback) callback();
6258
}
63-
else {
59+
else if (typeof(data) === 'string') {
6460
if (!mimeType)
6561
mimeType = Scenario.DEFAULT_TEXT_MIME_TYPE;
6662

67-
attachString(data.toString(), mimeType);
63+
attachData(data, mimeType);
6864
if (callback) callback();
65+
} else {
66+
throw Error(Scenario.ATTACH_INVALID_DATA_TYPE);
6967
}
7068
}
7169
};
@@ -76,5 +74,6 @@ function Scenario(astScenario, scenarioResult) {
7674
Scenario.DEFAULT_TEXT_MIME_TYPE = 'text/plain';
7775
Scenario.ATTACH_MISSING_MIME_TYPE_ARGUMENT = 'Cucumber.Api.Scenario.attach() expects a mimeType';
7876
Scenario.ATTACH_MISSING_CALLBACK_ARGUMENT_FOR_STREAM_READABLE = 'Cucumber.Api.Scenario.attach() expects a callback when data is a stream.Readable';
77+
Scenario.ATTACH_INVALID_DATA_TYPE = 'Cucumber.Api.Scenario.attach() expects data to be a stream, buffer, or string.';
7978

8079
module.exports = Scenario;

spec/cucumber/api/scenario_spec.js

+16-2
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ describe("Cucumber.Api.Scenario", function () {
217217
var attachments = scenario.getAttachments();
218218
expect(attachments.length).toEqual(1);
219219
var attachment = attachments[0];
220-
expect(attachment.getData()).toEqual("first chunksecond chunk");
220+
expect(attachment.getData().toString()).toEqual("first chunksecond chunk");
221221
expect(attachment.getMimeType()).toEqual(mimeType);
222222
});
223223

@@ -244,7 +244,7 @@ describe("Cucumber.Api.Scenario", function () {
244244
var attachments = scenario.getAttachments();
245245
expect(attachments.length).toEqual(1);
246246
var attachment = attachments[0];
247-
expect(attachment.getData()).toEqual("data");
247+
expect(attachment.getData().toString()).toEqual("data");
248248
expect(attachment.getMimeType()).toEqual(mimeType);
249249
});
250250

@@ -300,5 +300,19 @@ describe("Cucumber.Api.Scenario", function () {
300300
expect(callback).toHaveBeenCalled();
301301
});
302302
});
303+
304+
describe("when the data is an object", function () {
305+
var data;
306+
307+
beforeEach(function () {
308+
data = {};
309+
});
310+
311+
it("throws an error", function () {
312+
expect(function() {
313+
scenario.attach(data, mimeType);
314+
}).toThrow(new Error("Cucumber.Api.Scenario.attach() expects data to be a stream, buffer, or string."));
315+
});
316+
});
303317
});
304318
});

0 commit comments

Comments
 (0)