-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[FIX] Resource: getStream for empty string #249
Conversation
test/lib/adapters/FileSystem_read.js
Outdated
@@ -94,7 +94,7 @@ test("glob resources from application.a with directory exclude", async (t) => { | |||
}); | |||
|
|||
await readerWriter.byGlob("/!(pony,unicorn)/**").then(function(resources) { | |||
t.deepEqual(resources.length, 2, "Found exactly two resource"); | |||
t.deepEqual(resources.length, 3, "Found exactly two resource"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
t.deepEqual(resources.length, 3, "Found exactly two resource"); | |
t.deepEqual(resources.length, 3, "Found exactly three resource"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
test/lib/Resource.js
Outdated
@@ -160,6 +185,12 @@ test("Resource: clone resource with stream", (t) => { | |||
}); | |||
}); | |||
|
|||
test("getStream for empty file: correctly retrieved", async (t) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test is unrelated to your fix. It also passes without it. Why is it necessary? The commit message does not explain that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added it for completeness, there was no test for a stream of an empty file. Will add it to the commit message.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
will update message
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well I'm just wondering because half of the changes done in this PR are related to this test. And I'm not sure whether it tests any of our code.
In this test case, you pass in a stream (using the createStream
callback) and retrieve it using getStream
. The Resource implementation never handles the streams content. So it doesn't matter whether it's empty or not. Maybe I'm missing something but to me this test seems pretty useless?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
removed it
Also, this should be a |
If a resource does not have any content (empty string). The stream still can be retrieved. Before the error `Resource ${this._path} has no content` was thrown for the empty string.
Improve documentation and variable naming
Adjust wording
d10cecb
to
7457e45
Compare
remove test
test/lib/Resource.js
Outdated
@@ -4,10 +4,10 @@ const fs = require("fs"); | |||
const path = require("path"); | |||
const Resource = require("../../lib/Resource"); | |||
|
|||
function createBasicResource() { | |||
const fsPath = path.join("test", "fixtures", "application.a", "webapp", "index.html"); | |||
function createBasicResource(fileName = "index.html") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change is not necessary anymore
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
test/lib/adapters/FileSystem_read.js
Outdated
@@ -94,7 +94,7 @@ test("glob resources from application.a with directory exclude", async (t) => { | |||
}); | |||
|
|||
await readerWriter.byGlob("/!(pony,unicorn)/**").then(function(resources) { | |||
t.deepEqual(resources.length, 2, "Found exactly two resource"); | |||
t.deepEqual(resources.length, 3, "Found exactly three resource"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test/fixtures/application.a/webapp/empty.js can be removed again and this change reverted
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
removed unnecessary test content
lib/Resource.js
Outdated
@@ -73,7 +73,7 @@ class Resource { | |||
this._createStream = createStream || null; | |||
this._stream = stream || null; | |||
this._buffer = buffer || null; | |||
if (string) { | |||
if (typeof string === "string") { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we also check for string instanceof String
to catch cases where a new String("bla")
object is passed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would either use then both in combination:
typeof and instanceof
let simpleStr = 'This is a simple string'
let myString = new String()
let newStr = new String('String created with constructor')
let owString = String("asd")
simpleStr instanceof String // returns false, string literal is not an object
myString instanceof String // returns true
newStr instanceof String // returns true
owString instanceof String // returns false
(newStr + "a") instanceof String // returns false
newStr.toString() instanceof String // returns false
source + modified:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/instanceof
or leave typeof because that is rare that a string is constructed using the constructor with new
.
Also see
https://google.github.io/styleguide/jsguide.html#disallowed-features-wrapper-objects
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I would add that as an additional check as well. Just like here: https://stackoverflow.com/a/9436948
or leave typeof because that is rare that a string is constructed using the constructor with new.
Yes it's rare, but if somebody does it we would still create the resource but without any content. That might cause issues that are not easy to understand. We should add this check to prevent this from happening.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
kk, done
add check for string instance
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Please squash and merge, otherwise we will have 6 new entries in the changelog instead of one
t.is(result, "Content", "Stream has been read correctly"); | ||
}); | ||
}); | ||
|
||
test("Resource: getStream for empty string", async (t) => { | ||
t.plan(1); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary but doesn't hurt
If a resource does not have any content (empty string), the stream still can be retrieved.
Before the error
Resource ${this._path} has no content
was thrown for the empty string.
Add additional test "getStream for empty file: correctly retrieved" which uses a buffer for an empty file for completeness.