Skip to content
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

Merged
merged 6 commits into from
Jun 24, 2020
Merged

Conversation

tobiasso85
Copy link
Contributor

@tobiasso85 tobiasso85 commented Jun 17, 2020

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.

@CLAassistant
Copy link

CLAassistant commented Jun 17, 2020

CLA assistant check
All committers have signed the CLA.

@coveralls
Copy link

coveralls commented Jun 17, 2020

Coverage Status

Coverage increased (+0.03%) to 87.378% when pulling cf7d40b on resource-empty-string into b3eb22f on master.

@tobiasso85 tobiasso85 requested review from RandomByte and matz3 June 17, 2020 08:21
@@ -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");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
t.deepEqual(resources.length, 3, "Found exactly two resource");
t.deepEqual(resources.length, 3, "Found exactly three resource");

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -160,6 +185,12 @@ test("Resource: clone resource with stream", (t) => {
});
});

test("getStream for empty file: correctly retrieved", async (t) => {
Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

will update message

Copy link
Member

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removed it

@RandomByte
Copy link
Member

Also, this should be a [FIX] so that it is visible in the changelog

@tobiasso85 tobiasso85 requested a review from RandomByte June 19, 2020 12:52
@tobiasso85 tobiasso85 changed the title [INTERNAL] Resource: getStream for empty string [FIX] Resource: getStream for empty string Jun 22, 2020
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
@tobiasso85 tobiasso85 force-pushed the resource-empty-string branch from d10cecb to 7457e45 Compare June 22, 2020 07:27
@@ -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") {
Copy link
Member

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -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");
Copy link
Member

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

Copy link
Contributor Author

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
@tobiasso85 tobiasso85 requested a review from RandomByte June 22, 2020 13:30
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") {
Copy link
Member

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?

Copy link
Contributor Author

@tobiasso85 tobiasso85 Jun 23, 2020

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

Copy link
Member

@RandomByte RandomByte Jun 23, 2020

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.

Copy link
Contributor Author

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
Copy link
Member

@RandomByte RandomByte left a 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);
Copy link
Member

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

@tobiasso85 tobiasso85 merged commit bc5eafb into master Jun 24, 2020
@tobiasso85 tobiasso85 deleted the resource-empty-string branch June 24, 2020 07:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants