From 3b2da358b344bc618698e4e4b5dab3c44b37d064 Mon Sep 17 00:00:00 2001 From: Ethan Lane Date: Sat, 7 Dec 2024 22:21:43 +0000 Subject: [PATCH] Fix bug where you weren't able to use both the -q and -j flags at the same time (#227) - Fix bug where if you supplied both `-q`/`--query-metadata` and `-j`/`--json` the query metadata wasn't added to the output - Update tests #181 Reviewed-on: https://git.vylpes.xyz/RabbitLabs/random-bunny/pulls/227 Reviewed-by: VylpesTester Co-authored-by: Ethan Lane Co-committed-by: Ethan Lane --- src/helpers/outputHelper.ts | 40 ++++++++----------- .../__snapshots__/outputHelper.test.ts.snap | 34 +++++++++------- tests/helpers/outputHelper.test.ts | 35 ++++++++++++++++ 3 files changed, 70 insertions(+), 39 deletions(-) diff --git a/src/helpers/outputHelper.ts b/src/helpers/outputHelper.ts index 0cee9d6..976f7d9 100644 --- a/src/helpers/outputHelper.ts +++ b/src/helpers/outputHelper.ts @@ -5,36 +5,28 @@ export default class OutputHelper { public static GenerateOutput(response: IReturnResult, options: ICliOptions): string { const result = response.Result!; - const outputLines: string[] = []; + let outputObject = {}; + + outputObject = { ...result }; + + if (options.queryMetadata) { + outputObject = { ...outputObject, ...response.Query } + } if (options.json) { - return JSON.stringify(result); + return JSON.stringify(outputObject); } - outputLines.push(`Archived = ${result.Archived}`); - outputLines.push(`Author = ${result.Author}`); - outputLines.push(`Downvotes = ${result.Downs}`); + return this.GetFriendlyObjectText(outputObject); + } - if (result.Gallery.length > 1) { - outputLines.push(`Gallery = ${result.Gallery.join(", ")}`); + private static GetFriendlyObjectText(object: T): string { + const output: string[] = []; + + for (const key in object) { + output.push(`${key} = ${object[key]}`); } - outputLines.push(`Hidden = ${result.Hidden}`); - - outputLines.push(`Permalink = ${result.Permalink}`); - outputLines.push(`Subreddit = ${result.Subreddit}`); - outputLines.push(`Subreddit Subscribers = ${result.SubredditSubscribers}`); - outputLines.push(`Title = ${result.Title}`); - outputLines.push(`Upvotes = ${result.Ups}`); - outputLines.push(`Url = ${result.Url}`); - - - if (options.queryMetadata != null) { - outputLines.push(`Query.Subreddit = ${response.Query.subreddit}`); - outputLines.push(`Query.Sort By = ${response.Query.sortBy}`); - outputLines.push(`Query.Limit = ${response.Query.limit}`); - } - - return outputLines.join("\n"); + return output.join("\n"); } } \ No newline at end of file diff --git a/tests/helpers/__snapshots__/outputHelper.test.ts.snap b/tests/helpers/__snapshots__/outputHelper.test.ts.snap index 73d61c6..fa6efad 100644 --- a/tests/helpers/__snapshots__/outputHelper.test.ts.snap +++ b/tests/helpers/__snapshots__/outputHelper.test.ts.snap @@ -3,44 +3,48 @@ exports[`GenerateOutput EXPECT standout output to be returned 1`] = ` "Archived = false Author = author -Downvotes = 0 +Downs = 0 Hidden = false Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Subreddit = Rabbits -Subreddit Subscribers = 654751 +SubredditSubscribers = 654751 Title = This is my Ms Bear! -Upvotes = 17 -Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" +Ups = 17 +Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" `; exports[`GenerateOutput GIVEN options.json is true, EXPECT output to be returned as JSON 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","Gallery":["https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d"]}"`; +exports[`GenerateOutput GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format 1`] = `"{"Archived":false,"Author":"author","Downs":0,"Hidden":false,"Permalink":"/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/","Subreddit":"Rabbits","SubredditSubscribers":654751,"Title":"This is my Ms Bear!","Ups":17,"Url":"https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d","subreddit":"rabbits","sortBy":"hot","limit":100}"`; + exports[`GenerateOutput GIVEN options.queryMetadata is supplied, EXPECT query metadata to be added 1`] = ` "Archived = false Author = author -Downvotes = 0 +Downs = 0 Hidden = false Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Subreddit = Rabbits -Subreddit Subscribers = 654751 +SubredditSubscribers = 654751 Title = This is my Ms Bear! -Upvotes = 17 +Ups = 17 Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d -Query.Subreddit = rabbits -Query.Sort By = hot -Query.Limit = 100" +Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +subreddit = rabbits +sortBy = hot +limit = 100" `; exports[`GenerateOutput GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added 1`] = ` "Archived = false Author = author -Downvotes = 0 -Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d, https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +Downs = 0 Hidden = false Permalink = /r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/ Subreddit = Rabbits -Subreddit Subscribers = 654751 +SubredditSubscribers = 654751 Title = This is my Ms Bear! -Upvotes = 17 -Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" +Ups = 17 +Url = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d +Gallery = https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d,https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d" `; diff --git a/tests/helpers/outputHelper.test.ts b/tests/helpers/outputHelper.test.ts index 1581964..a6acb44 100644 --- a/tests/helpers/outputHelper.test.ts +++ b/tests/helpers/outputHelper.test.ts @@ -106,6 +106,41 @@ describe("GenerateOutput", () => { expect(result).toMatchSnapshot(); }); + test("GIVEN options.queryMetadata AND options.json is supplied, EXPECT query metadata to be in JSON format", () => { + // Arrange + const response = { + IsSuccess: true, + Query: { + subreddit: "rabbits", + sortBy: "hot", + limit: 100, + }, + Result: { + Archived: false, + Author: 'author', + Downs: 0, + Hidden: false, + Permalink: "/r/Rabbits/comments/1dj8pbt/this_is_my_ms_bear/", + Subreddit: "Rabbits", + SubredditSubscribers: 654751, + Title: "This is my Ms Bear!", + Ups: 17, + Url: "https://preview.redd.it/d5yno653zf7d1.jpg?width=640&crop=smart&auto=webp&s=5064d1caec3c12ac2855eb57ff131d0b313d5e9d", + }, + } as IReturnResult; + + const options = { + json: true, + queryMetadata: true, + } as ICliOptions; + + // Act + const result = OutputHelper.GenerateOutput(response, options); + + // Assert + expect(result).toMatchSnapshot(); + }); + test("GIVEN the Gallery input has more than 1 item, EXPECT Gallery line to be added", () => { // Arrange const response = {