Skip to content

Commit

Permalink
Support mix vcr.show task
Browse files Browse the repository at this point in the history
  • Loading branch information
parroty committed Mar 11, 2014
1 parent cb3124c commit 5a9b137
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 7 deletions.
30 changes: 27 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,10 +167,12 @@ The following tasks are added by including exvcr package.
- [mix vcr](#mix-vcr-show-cassettes)
- [mix vcr.delete](#mix-vcrdelete-delete-cassettes)
- [mix vcr.check](#mix-vcrcheck-check-cassettes)
- [mix vcr.show](#mix-vcrshow-show-cassettes)
- [mix vcr --help](#mix-vcr-help-help)

#### [mix vcr] Show cassettes
```Shell
$ mix vcr
Showing list of cassettes in [fixture/vcr_cassettes]
[File Name] [Last Update]
example_httpotion.json 2013/11/07 23:24:49
Expand All @@ -189,7 +191,7 @@ Showing list of cassettes in [fixture/custom_cassettes]
```

#### [mix vcr.delete] Delete cassettes
The [mix vcr.delete] task deletes the cassettes that contains the specified pattern in the file name.
The `mix vcr.delete` task deletes the cassettes that contains the specified pattern in the file name.
```Shell
$ mix vcr.delete ibrowse
Deleted example_ibrowse.json.
Expand All @@ -208,7 +210,7 @@ Deleted example_ibrowse_multiple.json.
If -a (--all) option is specified, all the cassetes in the specified folder becomes the target for delete.

#### [mix vcr.check] Check cassettes
The [mix vcr.check] shows how many times each cassette is applied while executing [mix test] tasks. It is intended for verifying the cassettes are properly used. `[Cassette Counts]` indicates the count that the pre-recorded json cassettes are applied. `[Server Counts]` indicates the count that server access is performed.
The `mix vcr.check` shows how many times each cassette is applied while executing `mix test` tasks. It is intended for verifying the cassettes are properly used. `[Cassette Counts]` indicates the count that the pre-recorded json cassettes are applied. `[Server Counts]` indicates the count that server access is performed.

```Shell
$ mix vcr.check
Expand All @@ -234,7 +236,7 @@ Showing hit counts of cassettes in [fixture/custom_cassettes]
response_mocking_regex.json 1 0
```

The target test file can be limited by specifying test files, as similar as [mix test] tasks.
The target test file can be limited by specifying test files, as similar as `mix test` tasks.

```Shell
$ mix vcr.check test/exvcr_test.exs
Expand All @@ -247,6 +249,23 @@ Showing hit counts of cassettes in [fixture/vcr_cassettes]
...
```

#### [mix vcr.show] Show cassettes
The `mix vcr.show` task displays the contents of cassettes json file.

```Shell
$ mix vcr.show fixture/vcr_cassettes/httpoison_get.json
[
{
"request": {
"url": "http://example.com",
"headers": [],
"method": "get",
"body": "",
"options": []
},
...
```
#### [mix vcr --help] Help
```Shell
$ mix vcr --help
Expand All @@ -270,6 +289,11 @@ Usage: mix vcr.check [options] [test-files]

-d (--dir) Specify vcr cassettes directory
-c (--custom) Specify custom cassettes directory

Usage: mix vcr.show [cassete-file-names]
Used to show cassette contents

-j (--json) Parse response body as json to display
```
Expand Down
6 changes: 3 additions & 3 deletions lib/exvcr/json.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ defmodule ExVCR.JSON do
"""
def load(file_name, custom_mode, adapter) do
case { File.exists?(file_name), custom_mode } do
{ true, _ } -> read_json_file(file_name, adapter)
{ true, _ } -> read_json_file(file_name) |> Enum.map(&adapter.convert_from_string/1)
{ false, true } -> raise ExVCR.FileNotFoundError.new(message: "cassette file \"#{file_name}\" not found")
{ false, _ } -> []
end
Expand All @@ -27,7 +27,7 @@ defmodule ExVCR.JSON do
@doc """
Reads and parse the json file located at the specified file_name.
"""
def read_json_file(file_name, adapter) do
File.read!(file_name) |> JSEX.decode! |> Enum.map(&adapter.convert_from_string/1)
def read_json_file(file_name) do
File.read!(file_name) |> JSEX.decode!
end
end
33 changes: 33 additions & 0 deletions lib/exvcr/task/show.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
defmodule ExVCR.Task.Show do
@moduledoc """
Handles [mix vcr.show] task execution.
"""

@doc """
Displays the contents of cassettes.
This method will called by the mix task.
"""
def run(files, options) do
Enum.each(files, &(print_file(&1, options)))
end

defp print_file(file, options) do
IO.puts "\e[32mShowing #{file}\e[m"
IO.puts "\e[32m**************************************\e[m"
json = File.read!(file)
IO.puts json |> JSEX.prettify! |> String.replace(~r/\\n/, "\n")
if options[:json] do
IO.puts "\n\e[32mShowing response body\e[m"
case extract_body(json) |> JSEX.prettify do
{:ok, body_json } -> IO.puts body_json
{:error, _} -> IO.puts "Parsing response body failed."
end
end
IO.puts "\e[32m**************************************\e[m"
end

defp extract_body(json) do
response = JSEX.decode!(json) |> List.first |> HashDict.new |> HashDict.fetch!("response")
response |> HashDict.new |> HashDict.fetch!("body")
end
end
5 changes: 5 additions & 0 deletions lib/exvcr/task/util.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ Usage: mix vcr.check [options] [test-files]
-d (--dir) Specify vcr cassettes directory
-c (--custom) Specify custom cassettes directory
Usage: mix vcr.show [cassete-file-names]
Used to show cassette contents
-j (--json) Parse response body as json to display
"""
end
end
18 changes: 17 additions & 1 deletion lib/mix/tasks.ex
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,12 @@ defmodule Mix.Tasks.Vcr do
end

defmodule Check do
@moduledoc """
Check how the recorded cassettes are used while executing [mix test] task.
"""
use Mix.Task

@doc "Entry point for [mix vcr.check] task"
@doc "Entry point for [mix vcr.check] task."
def run(args) do
{options, files, _} = OptionParser.parse(args, aliases: [d: :dir, c: :custom])
dirs = ExVCR.Task.Util.parse_basic_options(options)
Expand All @@ -62,4 +65,17 @@ defmodule Mix.Tasks.Vcr do
Mix.Task.run("test", files ++ ["--cover"])
end
end

defmodule Show do
@moduledoc """
Show the contents of the cassettes.
"""
use Mix.Task

@doc "Entry point for [mix vcr.show] task."
def run(args) do
{options, files, _} = OptionParser.parse(args, aliases: [j: :json])
ExVCR.Task.Show.run(files, options)
end
end
end

0 comments on commit 5a9b137

Please sign in to comment.