capybara-differ 0.1.0 → 0.1.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/README.md +52 -2
- data/lib/capybara-differ/version.rb +1 -1
- data/lib/capybara-differ.rb +25 -9
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a4e1b27115e8f67ca29f21a0e1e05c7efedbd627
|
4
|
+
data.tar.gz: d58903f136fcc969e60043271549dc8168c657a9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 97004fa0e5a4244f1d10f6ddb98a80d054f400c9e0e40167a796eb983c79f26f62d7bbfdaaac0a590d8dbe00877c8004fbf75b281e928f8e3ebcbf84cd24c676
|
7
|
+
data.tar.gz: 9311b8cc7da1de5520f0ccd149b4a72c32dd9c872093163f2f2ae6dde68487ae3d335eb214aeaf6c194d67eb9e95bfa43c9368a37c9e8f0885799d34e80bf2b8
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,32 @@
|
|
1
1
|
# Capybara::Differ
|
2
2
|
|
3
|
-
|
3
|
+
[](https://badge.fury.io/rb/capybara-differ)
|
4
|
+
[](https://travis-ci.org/kyamaguchi/capybara-differ)
|
5
|
+
|
6
|
+
Print the diff of snapshots with Capybara.
|
4
7
|
This will help refactoring of views.
|
5
8
|
|
9
|
+
## Idea
|
10
|
+
|
11
|
+
The main feature of this library is **diffing beautified htmls**.
|
12
|
+
|
13
|
+
You cannot always get nice diffs with 'git diff' or 'diff' command.
|
14
|
+
But this library gives them.
|
15
|
+
This library try to remove the concerns on diff of whitespaces, linebreaks.
|
16
|
+
|
17
|
+
Expected use cases are
|
18
|
+
|
19
|
+
* Refactoring views of Rails app
|
20
|
+
* Migration of erb -> haml
|
21
|
+
* Integration with [datagrid](https://github.com/bogdan/datagrid)
|
22
|
+
* Tracking sites
|
23
|
+
* visit -> save_page -> check_page
|
24
|
+
|
25
|
+
### Dependencies
|
26
|
+
|
27
|
+
* https://github.com/samg/diffy
|
28
|
+
* https://github.com/threedaymonk/htmlbeautifier
|
29
|
+
|
6
30
|
## Installation
|
7
31
|
|
8
32
|
Add this line to your application's Gemfile:
|
@@ -45,9 +69,35 @@ you will see the diff of latest snapshot and the first snapshot in _tmp/capybara
|
|
45
69
|
You can reset the target snapshot by removing files in _tmp/capybara/name_of_snapshot/_ .
|
46
70
|
Or remove the directory of snapshots.
|
47
71
|
|
72
|
+
### Options
|
73
|
+
|
74
|
+
#### Compare with the previous version
|
75
|
+
|
76
|
+
By default, the target snapshot is the first version.
|
77
|
+
|
78
|
+
You can compare with the previous version with the following option
|
79
|
+
|
80
|
+
```
|
81
|
+
page.check_page('name_of_snapshot', compare_with: :previous, selector: 'table')
|
82
|
+
```
|
83
|
+
|
84
|
+
#### Diffy
|
85
|
+
|
86
|
+
This library depends on [diffy](https://github.com/samg/diffy)
|
87
|
+
You can pass the options for diffy something like 'context'.
|
88
|
+
|
89
|
+
```
|
90
|
+
page.check_page('name_of_snapshot', selector: 'table', diffy: {context: 3})
|
91
|
+
```
|
92
|
+
|
93
|
+
And you can change the format of output with diffy with 'format' option. (default :color)
|
94
|
+
|
95
|
+
```
|
96
|
+
page.check_page('name_of_snapshot', selector: 'table', diffy: {format: :html_simple})
|
97
|
+
```
|
98
|
+
|
48
99
|
## TODO
|
49
100
|
|
50
|
-
- Add an option to compare with the previous snapshot
|
51
101
|
- Add an option to suppress log
|
52
102
|
- Others
|
53
103
|
|
data/lib/capybara-differ.rb
CHANGED
@@ -15,14 +15,16 @@ module Capybara
|
|
15
15
|
end
|
16
16
|
|
17
17
|
def compare
|
18
|
-
if @old_file_path == @new_file_path
|
18
|
+
if @old_file_path.nil? || @new_file_path.nil? || @old_file_path == @new_file_path
|
19
19
|
puts "There is no history of snapshots"
|
20
20
|
return ''
|
21
21
|
end
|
22
|
-
puts "Comparing two files
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
puts "Comparing two files" + (target_selector ? " with selector [#{target_selector}]" : '')
|
23
|
+
puts " #{@old_file_path}\n #{@new_file_path}" unless diffy_options[:include_diff_info]
|
24
|
+
old_beautified_html_path = beautified_html(@old_file_path)
|
25
|
+
new_beautified_html_path = beautified_html(@new_file_path)
|
26
|
+
diff = Diffy::Diff.new(old_beautified_html_path, new_beautified_html_path, diffy_options.merge(source: 'files'))
|
27
|
+
diff.to_s(diffy_options.fetch(:format, :color))
|
26
28
|
end
|
27
29
|
|
28
30
|
def beautified_html(file)
|
@@ -34,8 +36,13 @@ module Capybara
|
|
34
36
|
x.content = "\n#{x.content.strip}\n" if x.text?
|
35
37
|
end
|
36
38
|
|
37
|
-
|
38
|
-
|
39
|
+
node = doc.css(target_selector || default_selector)
|
40
|
+
raise("Couldn't find the selector [#{target_selector}]") if node.empty?
|
41
|
+
beautified_html = HtmlBeautifier.beautify(node.to_html)
|
42
|
+
|
43
|
+
beautified_html_path = file + '.beauty'
|
44
|
+
File.write(beautified_html_path, beautified_html)
|
45
|
+
beautified_html_path
|
39
46
|
end
|
40
47
|
|
41
48
|
private
|
@@ -43,6 +50,14 @@ module Capybara
|
|
43
50
|
def target_selector
|
44
51
|
@options.fetch(:selector, nil)
|
45
52
|
end
|
53
|
+
|
54
|
+
def default_selector
|
55
|
+
'body > *'
|
56
|
+
end
|
57
|
+
|
58
|
+
def diffy_options
|
59
|
+
{context: 2, include_diff_info: true}.merge(@options.fetch(:diffy, {}))
|
60
|
+
end
|
46
61
|
end
|
47
62
|
|
48
63
|
def check_page(name, options = {})
|
@@ -50,8 +65,9 @@ module Capybara
|
|
50
65
|
save_page(filename)
|
51
66
|
|
52
67
|
base_dir = File.join([Capybara.save_path, name].compact)
|
53
|
-
|
54
|
-
|
68
|
+
file_list = Dir[File.join(base_dir, '*.html')]
|
69
|
+
old_html_path = options[:compare_with] == :previous ? file_list[-2] : file_list.first
|
70
|
+
new_html_path = file_list.last
|
55
71
|
|
56
72
|
comparator = Capybara::Differ::Comparator.new(old_html_path, new_html_path, options)
|
57
73
|
if (result = comparator.compare).strip.size > 0
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: capybara-differ
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kazuho Yamaguchi
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-04-
|
11
|
+
date: 2018-04-05 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: capybara
|