-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: Add tests for command line usage (#204)
- Loading branch information
Showing
11 changed files
with
237 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/** | ||
* This test checks the ability to invoke a tweet using the `--file` command line flag | ||
* but provides a missing filename so generates an error. | ||
*/ | ||
|
||
const nock = require("nock"); | ||
const tap = require("tap"); | ||
|
||
// MOCK | ||
process.chdir(__dirname); | ||
process.argv = [ | ||
"node", | ||
"lib/index.js", | ||
"--file", | ||
"tweets/does-not-exist.tweet", | ||
]; | ||
|
||
process.on("exit", (code) => { | ||
tap.equal(code, 1); | ||
tap.same(nock.pendingMocks(), []); | ||
|
||
// above code exits with 1 (error), but tap expects 0. | ||
// Tap adds the "process.exitCode" property for that purpose. | ||
process.exitCode = 0; | ||
}); | ||
|
||
require("../../lib"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/** | ||
* This test checks the ability to invoke a tweet using the `--file` command line flag | ||
* but provides no actual filename so generates an error. | ||
*/ | ||
|
||
const nock = require("nock"); | ||
const tap = require("tap"); | ||
|
||
// MOCK | ||
process.chdir(__dirname); | ||
process.argv = ["node", "lib/index.js", "--file"]; | ||
|
||
process.on("exit", (code) => { | ||
tap.equal(code, 1); | ||
tap.same(nock.pendingMocks(), []); | ||
|
||
// above code exits with 1 (error), but tap expects 0. | ||
// Tap adds the "process.exitCode" property for that purpose. | ||
process.exitCode = 0; | ||
}); | ||
|
||
require("../../lib"); |
Binary file added
BIN
+105 KB
...and-line-has-tweet-with-front-matter-media-in-directory/custom/media/blahaj.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
7 changes: 7 additions & 0 deletions
7
...mmand-line-has-tweet-with-front-matter-media-in-directory/custom/tweets/hello-world.tweet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
media: | ||
- file: blahaj.png | ||
alt: Blahaj! | ||
--- | ||
|
||
Cuddly :) |
74 changes: 74 additions & 0 deletions
74
test/command-line-has-tweet-with-front-matter-media-in-directory/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
/** | ||
* This test checks the ability to invoke a tweet using the `--file` command line flag | ||
* which is in a custom directory and has front matter that includes media. | ||
*/ | ||
|
||
const nock = require("nock"); | ||
const tap = require("tap"); | ||
|
||
// MOCK | ||
nock("https://api.twitter.com") | ||
.post("/1.1/statuses/update.json", (body) => { | ||
tap.equal(body.status, "Cuddly :)\n"); | ||
tap.equal(body.media_ids, "0000000000000000002"); | ||
return true; | ||
}) | ||
.reply(201, { | ||
id_str: "0000000000000000001", | ||
user: { | ||
screen_name: "gr2m", | ||
}, | ||
}); | ||
|
||
nock("https://upload.twitter.com") | ||
.post("/1.1/media/upload.json", (body) => { | ||
tap.equal(body.command, "INIT"); | ||
tap.equal(body.total_bytes, "107352"); | ||
tap.equal(body.media_type, "image/png"); | ||
return true; | ||
}) | ||
.reply(201, { | ||
media_id_string: "0000000000000000002", | ||
}) | ||
|
||
.post("/1.1/media/upload.json", () => { | ||
// TODO: Body just seems to be a string here, not an object. | ||
// tap.equal(body.command, "APPEND"); | ||
// tap.equal(body.media_id, "0000000000000000002"); | ||
// tap.equal(body.media_type, ""); | ||
// tap.equal(body.media, ""); | ||
// tap.equal(body.segment_index, 0); | ||
return true; | ||
}) | ||
.reply(201) | ||
|
||
.post("/1.1/media/upload.json", (body) => { | ||
tap.equal(body.command, "FINALIZE"); | ||
tap.equal(body.media_id, "0000000000000000002"); | ||
return true; | ||
}) | ||
.reply(201); | ||
|
||
// TODO: Support alt text (twitter library does not support JSON payloads) | ||
// https://github.com/desmondmorris/node-twitter/issues/347 | ||
// .post("/1.1/media/metadata/create.json", (body) => { | ||
// tap.equal(body.media_id, "0000000000000000002"); | ||
// tap.equal(body.alt_text.text, "Blahaj!"); | ||
// return true; | ||
// }) | ||
// .reply(201); | ||
|
||
process.chdir(__dirname); | ||
process.argv = [ | ||
"node", | ||
"lib/index.js", | ||
"--file", | ||
"custom/tweets/hello-world.tweet", | ||
]; | ||
|
||
process.on("exit", (code) => { | ||
tap.equal(code, 0); | ||
tap.same(nock.pendingMocks(), []); | ||
}); | ||
|
||
require("../../lib"); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
69 changes: 69 additions & 0 deletions
69
test/command-line-has-tweet-with-front-matter-media/test.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* This test checks the ability to invoke a tweet using the `--file` command line flag | ||
* which has front matter that includes media. | ||
*/ | ||
|
||
const nock = require("nock"); | ||
const tap = require("tap"); | ||
|
||
// MOCK | ||
nock("https://api.twitter.com") | ||
.post("/1.1/statuses/update.json", (body) => { | ||
tap.equal(body.status, "Cuddly :)\n"); | ||
tap.equal(body.media_ids, "0000000000000000002"); | ||
return true; | ||
}) | ||
.reply(201, { | ||
id_str: "0000000000000000001", | ||
user: { | ||
screen_name: "gr2m", | ||
}, | ||
}); | ||
|
||
nock("https://upload.twitter.com") | ||
.post("/1.1/media/upload.json", (body) => { | ||
tap.equal(body.command, "INIT"); | ||
tap.equal(body.total_bytes, "107352"); | ||
tap.equal(body.media_type, "image/png"); | ||
return true; | ||
}) | ||
.reply(201, { | ||
media_id_string: "0000000000000000002", | ||
}) | ||
|
||
.post("/1.1/media/upload.json", () => { | ||
// TODO: Body just seems to be a string here, not an object. | ||
// tap.equal(body.command, "APPEND"); | ||
// tap.equal(body.media_id, "0000000000000000002"); | ||
// tap.equal(body.media_type, ""); | ||
// tap.equal(body.media, ""); | ||
// tap.equal(body.segment_index, 0); | ||
return true; | ||
}) | ||
.reply(201) | ||
|
||
.post("/1.1/media/upload.json", (body) => { | ||
tap.equal(body.command, "FINALIZE"); | ||
tap.equal(body.media_id, "0000000000000000002"); | ||
return true; | ||
}) | ||
.reply(201); | ||
|
||
// TODO: Support alt text (twitter library does not support JSON payloads) | ||
// https://github.com/desmondmorris/node-twitter/issues/347 | ||
// .post("/1.1/media/metadata/create.json", (body) => { | ||
// tap.equal(body.media_id, "0000000000000000002"); | ||
// tap.equal(body.alt_text.text, "Blahaj!"); | ||
// return true; | ||
// }) | ||
// .reply(201); | ||
|
||
process.chdir(__dirname); | ||
process.argv = ["node", "lib/index.js", "--file", "tweets/hello-world.tweet"]; | ||
|
||
process.on("exit", (code) => { | ||
tap.equal(code, 0); | ||
tap.same(nock.pendingMocks(), []); | ||
}); | ||
|
||
require("../../lib"); |
7 changes: 7 additions & 0 deletions
7
test/command-line-has-tweet-with-front-matter-media/tweets/hello-world.tweet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
--- | ||
media: | ||
- file: blahaj.png | ||
alt: Blahaj! | ||
--- | ||
|
||
Cuddly :) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
/** | ||
* This test checks the ability to invoke a tweet using the `--file` command line flag. | ||
*/ | ||
|
||
const nock = require("nock"); | ||
const tap = require("tap"); | ||
|
||
// MOCK | ||
nock("https://api.twitter.com") | ||
.post("/1.1/statuses/update.json", (body) => { | ||
tap.equal(body.status, "Hello, world!\n"); | ||
return true; | ||
}) | ||
.reply(201, { | ||
id_str: "0000000000000000001", | ||
user: { | ||
screen_name: "gr2m", | ||
}, | ||
}); | ||
|
||
process.chdir(__dirname); | ||
process.argv = ["node", "lib/index.js", "--file", "tweets/hello-world.tweet"]; | ||
|
||
process.on("exit", (code) => { | ||
tap.equal(code, 0); | ||
tap.same(nock.pendingMocks(), []); | ||
}); | ||
|
||
require("../../lib"); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Hello, world! |