cmpfs-ruby 0.2.0 → 0.2.1.2
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 +5 -5
- data/LICENSE +17 -16
- data/README.md +133 -11
- data/examples/compare_two_binary_files.rb +44 -0
- data/examples/compare_two_text_files.rb +44 -0
- data/lib/cmpfs/compare/api_1_9.rb +142 -120
- data/lib/cmpfs/compare/api_2.rb +130 -108
- data/lib/cmpfs/compare/binary/internal_.rb +50 -52
- data/lib/cmpfs/compare/text/internal_.rb +89 -92
- data/lib/cmpfs/compare.rb +25 -23
- data/lib/cmpfs/version.rb +20 -19
- data/lib/cmpfs.rb +22 -18
- data/test/unit/compare/tc_compare_binary.rb +52 -51
- data/test/unit/compare/ts_all.rb +1 -1
- data/test/unit/tc_compare_binary.rb +53 -54
- data/test/unit/tc_compare_text.rb +85 -31
- data/test/unit/tc_version.rb +19 -16
- data/test/unit/ts_all.rb +1 -1
- metadata +24 -17
data/lib/cmpfs/compare/api_2.rb
CHANGED
@@ -1,127 +1,149 @@
|
|
1
1
|
|
2
2
|
unless RUBY_VERSION >= '2'
|
3
3
|
|
4
|
-
|
4
|
+
abort "This file required Ruby 2+: RUBY_VERSION='#{RUBY_VERSION}'"
|
5
5
|
end
|
6
6
|
|
7
7
|
require 'cmpfs/compare/binary/internal_'
|
8
8
|
require 'cmpfs/compare/text/internal_'
|
9
9
|
|
10
|
+
|
10
11
|
module CmpFS
|
11
12
|
module Compare
|
12
13
|
|
13
14
|
module CmpFS_Compare_Methods
|
14
15
|
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
16
|
+
# Compares two files, named by +lhs_path+ and +rhs_path+, in a binary
|
17
|
+
# (exact) manner
|
18
|
+
#
|
19
|
+
# === Signature
|
20
|
+
#
|
21
|
+
# * *Parameters:*
|
22
|
+
# - +lhs_path+:: (+String+) The name of a file
|
23
|
+
# - +rhs_path+:: (+String+) The name of a file
|
24
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
25
|
+
#
|
26
|
+
# * *Options:*
|
27
|
+
#
|
28
|
+
# === Return
|
29
|
+
# +true+ if the files have exactly the same content; +false+ otherwise.
|
30
|
+
def compare_binary_files lhs_path, rhs_path, **options
|
31
|
+
|
32
|
+
::CmpFS::Compare::Binary::Internal_.compare_binary_files_ lhs_path, rhs_path, options
|
33
|
+
end
|
34
|
+
|
35
|
+
# Compares two streams, +lhs_stm+ and +rhs_stm+, in a binary
|
36
|
+
# (exact) manner
|
37
|
+
#
|
38
|
+
# === Signature
|
39
|
+
#
|
40
|
+
# * *Parameters:*
|
41
|
+
# - +lhs_stm+:: (stream) A stream object
|
42
|
+
# - +rhs_stm+:: (stream) A stream object
|
43
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
44
|
+
#
|
45
|
+
# * *Options:*
|
46
|
+
# - +:no_rewind+:: (boolean) Prevents the default behaviour of rewinding each stream before processing
|
47
|
+
#
|
48
|
+
# === Return
|
49
|
+
# +true+ if the streams have exactly the same content; +false+ otherwise.
|
50
|
+
def compare_binary_streams lhs_stm, rhs_stm, **options
|
51
|
+
|
52
|
+
::CmpFS::Compare::Binary::Internal_.compare_binary_streams_ lhs_stm, rhs_stm, options
|
53
|
+
end
|
54
|
+
|
55
|
+
# Compares two files/streams in a binary (exact) manner
|
56
|
+
#
|
57
|
+
# === Signature
|
58
|
+
#
|
59
|
+
# * *Parameters:*
|
60
|
+
# - +lhs+:: (+String+, stream) The name of a file, or a stream object
|
61
|
+
# - +rhs+:: (+String+, stream) The name of a file, or a stream object
|
62
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
63
|
+
#
|
64
|
+
# * *Options:*
|
65
|
+
# - +:no_rewind+:: (boolean) Prevents the default behaviour of rewinding each stream before processing
|
66
|
+
#
|
67
|
+
# === Return
|
68
|
+
# +true+ if the files/streams have exactly the same content; +false+
|
69
|
+
# otherwise.
|
70
|
+
def compare_binary lhs, rhs, **options
|
71
|
+
|
72
|
+
::CmpFS::Compare::Binary::Internal_.compare_binary_ lhs, rhs, options
|
73
|
+
end
|
74
|
+
|
75
|
+
|
76
|
+
# Compares two files, named by +lhs_path+ and +rhs_path+, in a textual
|
77
|
+
# manner according to the given +options+
|
78
|
+
#
|
79
|
+
# === Signature
|
80
|
+
#
|
81
|
+
# * *Parameters:*
|
82
|
+
# - +lhs_path+:: (+String+) The name of a file
|
83
|
+
# - +rhs_path+:: (+String+) The name of a file
|
84
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
85
|
+
#
|
86
|
+
# * *Options:*
|
87
|
+
# - +:skip_blank_lines+:: (boolean) Determines whether blank lines should be skipped from the comparison
|
88
|
+
# - +:trim_lines+:: (boolean) Determines whether lines should be trimmed of leading and trailing space (including EOL sequence)
|
89
|
+
#
|
90
|
+
# === Return
|
91
|
+
# +true+ if the files/streams have exactly the same content; +false+
|
92
|
+
# otherwise.
|
93
|
+
def compare_text_files lhs_path, rhs_path, **options
|
94
|
+
|
95
|
+
::CmpFS::Compare::Text::Internal_.compare_text_files_ lhs_path, rhs_path, options
|
96
|
+
end
|
97
|
+
|
98
|
+
# Compares two streams, named by +lhs_stm+ and +rhs_stm+, in a textual
|
99
|
+
# manner according to the given +options+
|
100
|
+
#
|
101
|
+
# === Signature
|
102
|
+
#
|
103
|
+
# * *Parameters:*
|
104
|
+
# - +lhs_stm+:: (stream) A stream object
|
105
|
+
# - +rhs_stm+:: (stream) A stream object
|
106
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
107
|
+
#
|
108
|
+
# * *Options:*
|
109
|
+
# - +:no_rewind+:: (boolean) Prevents the default behaviour of rewinding each stream before processing
|
110
|
+
# - +:skip_blank_lines+:: (boolean) Determines whether blank lines should be skipped from the comparison
|
111
|
+
# - +:trim_lines+:: (boolean) Determines whether lines should be trimmed of leading and trailing space (including EOL sequence)
|
112
|
+
#
|
113
|
+
# === Return
|
114
|
+
# +true+ if the files/streams have exactly the same content; +false+
|
115
|
+
# otherwise.
|
116
|
+
def compare_text_streams lhs_stm, rhs_stm, **options
|
117
|
+
|
118
|
+
::CmpFS::Compare::Text::Internal_.compare_text_streams_ lhs_stm, rhs_stm, options
|
119
|
+
end
|
120
|
+
|
121
|
+
# Compares two files/streams, named by +lhs+ and +rhs+, in a textual
|
122
|
+
# manner according to the given +options+
|
123
|
+
#
|
124
|
+
# === Signature
|
125
|
+
#
|
126
|
+
# * *Parameters:*
|
127
|
+
# - +lhs+:: (+String+, stream) The name of a file, or a stream object
|
128
|
+
# - +rhs+:: (+String+, stream) The name of a file, or a stream object
|
129
|
+
# - +options+:: (+Hash+) Options that control the behaviour of the method
|
130
|
+
#
|
131
|
+
# * *Options:*
|
132
|
+
# - +:no_rewind+:: (boolean) Prevents the default behaviour of rewinding each stream before processing
|
133
|
+
# - +:skip_blank_lines+:: (boolean) Determines whether blank lines should be skipped from the comparison
|
134
|
+
# - +:trim_lines+:: (boolean) Determines whether lines should be trimmed of leading and trailing space (including EOL sequence)
|
135
|
+
#
|
136
|
+
# === Return
|
137
|
+
# +true+ if the files/streams have exactly the same content; +false+
|
138
|
+
# otherwise.
|
139
|
+
def compare_text lhs, rhs, **options
|
140
|
+
|
141
|
+
::CmpFS::Compare::Text::Internal_.compare_text_ lhs, rhs, options
|
142
|
+
end
|
121
143
|
end # module CmpFS_Compare_Methods
|
122
144
|
end # module Compare
|
123
145
|
end # module CmpFS
|
124
146
|
|
125
|
-
# ############################## end of file ############################# #
|
126
147
|
|
148
|
+
# ############################## end of file ############################# #
|
127
149
|
|
@@ -1,89 +1,87 @@
|
|
1
1
|
|
2
|
-
#require 'xqsr3'
|
3
|
-
|
4
2
|
require 'fileutils'
|
5
3
|
require 'stringio'
|
6
4
|
|
5
|
+
|
7
6
|
module CmpFS
|
8
7
|
module Compare
|
9
8
|
module Binary
|
10
9
|
|
11
10
|
module Internal_
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
case p
|
16
|
-
when ::IO, ::StringIO
|
12
|
+
def self.determine_param_type_ p
|
17
13
|
|
18
|
-
|
19
|
-
|
14
|
+
case p
|
15
|
+
when ::IO, ::StringIO
|
20
16
|
|
21
|
-
|
22
|
-
|
17
|
+
return :io
|
18
|
+
when ::String
|
23
19
|
|
24
|
-
|
20
|
+
return :path
|
21
|
+
else
|
25
22
|
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
23
|
+
return :path if p.respond_to?(:to_str)
|
29
24
|
|
30
|
-
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
end
|
29
|
+
def self.compare_binary_files_ lhs_path, rhs_path, options
|
34
30
|
|
35
|
-
|
31
|
+
FileUtils.compare_file lhs_path, rhs_path
|
32
|
+
end
|
36
33
|
|
37
|
-
|
38
|
-
end
|
34
|
+
def self.compare_binary_streams_ lhs_stm, rhs_stm, options
|
39
35
|
|
40
|
-
|
36
|
+
FileUtils.compare_stream lhs_stm, rhs_stm
|
37
|
+
end
|
41
38
|
|
39
|
+
def self.compare_binary_ lhs, rhs, options
|
42
40
|
|
43
|
-
|
44
|
-
|
41
|
+
lhs_type = self.determine_param_type_ lhs
|
42
|
+
rhs_type = self.determine_param_type_ rhs
|
45
43
|
|
46
|
-
|
47
|
-
|
44
|
+
raise ArgumentError, "lhs is of unsupported type '#{lhs.class}'" unless lhs_type
|
45
|
+
raise ArgumentError, "rhs is of unsupported type '#{rhs.class}'" unless rhs_type
|
48
46
|
|
49
|
-
|
47
|
+
if lhs_type == rhs_type
|
50
48
|
|
51
|
-
|
52
|
-
|
49
|
+
case lhs_type
|
50
|
+
when :io
|
53
51
|
|
54
|
-
|
55
|
-
|
52
|
+
return self.compare_binary_streams_ lhs, rhs, options
|
53
|
+
when :path
|
56
54
|
|
57
|
-
|
58
|
-
|
59
|
-
|
55
|
+
return self.compare_binary_files_ lhs, rhs, options
|
56
|
+
end
|
57
|
+
else
|
60
58
|
|
61
|
-
|
62
|
-
|
59
|
+
case lhs_type
|
60
|
+
when :io
|
63
61
|
|
64
|
-
|
62
|
+
if :path == rhs_type
|
65
63
|
|
66
|
-
|
64
|
+
File.open(rhs, 'rb') do |rhs_f|
|
67
65
|
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
66
|
+
return self.compare_binary_streams_ lhs, rhs_f, options
|
67
|
+
end
|
68
|
+
end
|
69
|
+
when :path
|
72
70
|
|
73
|
-
|
71
|
+
if :path == lhs_type
|
74
72
|
|
75
|
-
|
73
|
+
File.open(lhs, 'rb') do |lhs_f|
|
76
74
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
75
|
+
return self.compare_binary_streams_ lhs_f, rhs, options
|
76
|
+
end
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
82
80
|
|
83
|
-
|
81
|
+
warn "#{__method__}: incompatible types (#{lhs.type}, #{rhs.type})"
|
84
82
|
|
85
|
-
|
86
|
-
|
83
|
+
nil
|
84
|
+
end
|
87
85
|
|
88
86
|
end # module Internal_
|
89
87
|
|
@@ -91,6 +89,6 @@ end # module Binary
|
|
91
89
|
end # module Compare
|
92
90
|
end # module CmpFS
|
93
91
|
|
94
|
-
# ############################## end of file ############################# #
|
95
92
|
|
93
|
+
# ############################## end of file ############################# #
|
96
94
|
|
@@ -1,159 +1,156 @@
|
|
1
1
|
|
2
|
-
#require 'xqsr3'
|
3
|
-
|
4
2
|
require 'fileutils'
|
5
3
|
require 'stringio'
|
6
4
|
|
5
|
+
|
7
6
|
module CmpFS
|
8
7
|
module Compare
|
9
8
|
module Text
|
10
9
|
|
11
10
|
module Internal_
|
12
11
|
|
13
|
-
|
14
|
-
|
15
|
-
case p
|
16
|
-
when ::IO, ::StringIO
|
12
|
+
def self.determine_param_type_ p
|
17
13
|
|
18
|
-
|
19
|
-
|
14
|
+
case p
|
15
|
+
when ::IO, ::StringIO
|
20
16
|
|
21
|
-
|
22
|
-
|
17
|
+
return :io
|
18
|
+
when ::String
|
23
19
|
|
24
|
-
|
20
|
+
return :path
|
21
|
+
else
|
25
22
|
|
26
|
-
|
27
|
-
end
|
28
|
-
end
|
23
|
+
return :path if p.respond_to?(:to_str)
|
29
24
|
|
30
|
-
|
25
|
+
return nil
|
26
|
+
end
|
27
|
+
end
|
31
28
|
|
32
|
-
|
33
|
-
skipping_blanks = options[:skip_blank_lines]
|
29
|
+
def self.next_line_or_nil_ en, options
|
34
30
|
|
35
|
-
|
31
|
+
skipping_blanks = options[:skip_blank_lines]
|
32
|
+
trimming_lines = options[:trim_lines]
|
36
33
|
|
37
|
-
|
34
|
+
num_read = 0
|
38
35
|
|
39
|
-
|
36
|
+
begin
|
40
37
|
|
41
|
-
|
38
|
+
loop do
|
42
39
|
|
43
|
-
|
40
|
+
line = en.next
|
44
41
|
|
45
|
-
|
42
|
+
num_read += 1
|
46
43
|
|
47
|
-
|
44
|
+
line = line.strip if trimming_lines
|
48
45
|
|
49
|
-
|
50
|
-
end
|
46
|
+
if line.empty? && skipping_blanks
|
51
47
|
|
52
|
-
|
53
|
-
|
54
|
-
rescue StopIteration
|
48
|
+
next
|
49
|
+
end
|
55
50
|
|
56
|
-
|
51
|
+
return [ line, num_read ]
|
52
|
+
end
|
53
|
+
rescue StopIteration
|
57
54
|
|
58
|
-
|
59
|
-
end
|
55
|
+
end
|
60
56
|
|
61
|
-
|
57
|
+
return [ nil, num_read ]
|
58
|
+
end
|
62
59
|
|
63
|
-
|
60
|
+
def self.compare_text_files_ lhs_path, rhs_path, options
|
64
61
|
|
65
|
-
|
62
|
+
File.open(lhs_path, 'r') do |lhs_stm|
|
66
63
|
|
67
|
-
|
68
|
-
end
|
69
|
-
end
|
70
|
-
end
|
64
|
+
File.open(rhs_path, 'r') do |rhs_stm|
|
71
65
|
|
72
|
-
|
66
|
+
self.compare_text_streams_ lhs_stm, rhs_stm, options.merge(no_rewind: true)
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
73
70
|
|
74
|
-
|
75
|
-
rhs_en = rhs_stm.each_line
|
71
|
+
def self.compare_text_streams_ lhs_stm, rhs_stm, options
|
76
72
|
|
77
|
-
|
73
|
+
lhs_en = lhs_stm.each_line
|
74
|
+
rhs_en = rhs_stm.each_line
|
78
75
|
|
79
|
-
|
80
|
-
rhs_en.rewind if rhs_stm.respond_to?(:rewind)
|
81
|
-
end
|
76
|
+
unless options[:no_rewind]
|
82
77
|
|
83
|
-
|
84
|
-
|
78
|
+
lhs_en.rewind if lhs_stm.respond_to?(:rewind)
|
79
|
+
rhs_en.rewind if rhs_stm.respond_to?(:rewind)
|
80
|
+
end
|
85
81
|
|
86
|
-
|
82
|
+
lhs_ix = 0
|
83
|
+
rhs_ix = 0
|
87
84
|
|
88
|
-
|
89
|
-
rhs_ln, rhs_nr = self.next_line_or_nil_ rhs_en, options
|
85
|
+
loop do
|
90
86
|
|
91
|
-
|
87
|
+
lhs_ln, lhs_nr = self.next_line_or_nil_ lhs_en, options
|
88
|
+
rhs_ln, rhs_nr = self.next_line_or_nil_ rhs_en, options
|
92
89
|
|
93
|
-
|
94
|
-
else
|
90
|
+
if lhs_ln != rhs_ln
|
95
91
|
|
96
|
-
|
97
|
-
|
98
|
-
end
|
92
|
+
return false
|
93
|
+
else
|
99
94
|
|
100
|
-
|
101
|
-
|
95
|
+
return true if lhs_ln.nil?
|
96
|
+
end
|
97
|
+
end
|
102
98
|
|
103
|
-
|
99
|
+
return true
|
100
|
+
end
|
104
101
|
|
105
|
-
|
106
|
-
rhs_type = self.determine_param_type_ rhs
|
102
|
+
def self.compare_text_ lhs, rhs, options
|
107
103
|
|
108
|
-
|
109
|
-
|
104
|
+
lhs_type = self.determine_param_type_ lhs
|
105
|
+
rhs_type = self.determine_param_type_ rhs
|
110
106
|
|
111
|
-
|
107
|
+
raise ArgumentError, "lhs is of unsupported type '#{lhs.class}'" unless lhs_type
|
108
|
+
raise ArgumentError, "rhs is of unsupported type '#{rhs.class}'" unless rhs_type
|
112
109
|
|
113
|
-
|
114
|
-
when :io
|
110
|
+
if lhs_type == rhs_type
|
115
111
|
|
116
|
-
|
117
|
-
|
112
|
+
case lhs_type
|
113
|
+
when :io
|
118
114
|
|
119
|
-
|
120
|
-
|
121
|
-
else
|
115
|
+
return self.compare_text_streams_ lhs, rhs, options
|
116
|
+
when :path
|
122
117
|
|
123
|
-
|
124
|
-
|
118
|
+
return self.compare_text_files_ lhs, rhs, options
|
119
|
+
end
|
120
|
+
else
|
125
121
|
|
126
|
-
|
122
|
+
case lhs_type
|
123
|
+
when :io
|
127
124
|
|
128
|
-
|
125
|
+
if :path == rhs_type
|
129
126
|
|
130
|
-
|
131
|
-
end
|
132
|
-
end
|
133
|
-
when :path
|
127
|
+
File.open(rhs, 'r') do |rhs_f|
|
134
128
|
|
135
|
-
|
129
|
+
return self.compare_text_streams_ lhs, rhs_f, options
|
130
|
+
end
|
131
|
+
end
|
132
|
+
when :path
|
136
133
|
|
137
|
-
|
134
|
+
if :path == lhs_type
|
138
135
|
|
139
|
-
|
140
|
-
end
|
141
|
-
end
|
142
|
-
end
|
143
|
-
end
|
136
|
+
File.open(lhs, 'r') do |lhs_f|
|
144
137
|
|
145
|
-
|
138
|
+
return self.compare_text_streams_ lhs_f, rhs, options
|
139
|
+
end
|
140
|
+
end
|
141
|
+
end
|
142
|
+
end
|
146
143
|
|
147
|
-
|
148
|
-
end
|
144
|
+
warn "#{__method__}: incompatible types (#{lhs.type}, #{rhs.type})"
|
149
145
|
|
146
|
+
nil
|
147
|
+
end
|
150
148
|
end # module Internal_
|
151
149
|
|
152
150
|
end # module Text
|
153
151
|
end # module Compare
|
154
152
|
end # module CmpFS
|
155
153
|
|
156
|
-
# ############################## end of file ############################# #
|
157
|
-
|
158
154
|
|
155
|
+
# ############################## end of file ############################# #
|
159
156
|
|