diff options
| author | Teddy Wing | 2017-11-14 15:58:16 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2017-11-14 16:04:51 +0100 | 
| commit | 42fdf544a1beca0edd418408f31f448d54f91ecc (patch) | |
| tree | 6e26514f67fbeaeeae8418819721b0bc11465529 /spec/db | |
| parent | e2016fd20b77b6adef7e7da8a50ea4aa97e43dc3 (diff) | |
| download | chouette-core-42fdf544a1beca0edd418408f31f448d54f91ecc.tar.bz2 | |
schema_spec: Show helpful diff
Change the matching logic to support better diff output. Here, we
construct two in-memory string versions of the `schema.rb` file, one
with the original content, and another with the pseudo-expected values.
Of course, the expected values are generated, not real, but this at
least gives devs and idea of what the problem is. Additionally, it also
gives the line numbers where the expectation fails from RSpec's default
multi-line string differ.
This version looks at the file line-by-line instead of streaming it and
feeding it through `#grep` and `#grep_v`, so I imagine performance is
worse (assuming Ruby uses C-implementations for those functions), but as
far as I know there's no way to get line numbers using `#grep`. Since
the `schema.rb` file is generally small relatively speaking, this should
be fine.
Refs #4951
Diffstat (limited to 'spec/db')
| -rw-r--r-- | spec/db/schema_spec.rb | 35 | 
1 files changed, 22 insertions, 13 deletions
| diff --git a/spec/db/schema_spec.rb b/spec/db/schema_spec.rb index a36d3f926..92c553205 100644 --- a/spec/db/schema_spec.rb +++ b/spec/db/schema_spec.rb @@ -7,22 +7,31 @@ end  RSpec::Matchers.define :use_bigint_keys do    match do |filename| -    @non_bigint_primary_keys = [] -    @non_bigint_foreign_keys = [] +    @original = "" +    @expected = ""      File.open(filename, 'r') do |f| -      @non_bigint_primary_keys = f -        .grep(/create_table /) -        .grep_v(/id: (:bigserial|false)/) - -      f.rewind - -      @non_bigint_foreign_keys = f -        .grep(/t\.integer +"\w+_id"/) -        .grep_v(/limit: 8/) +      f.each_line do |line| +        expected_line = line + +        # Primary key +        if line =~ /create_table / && +          !(line =~ /id: (:bigserial|false)/) +          expected_line = line.sub(/(create_table "\w+", )/, '\1id: :bigserial, ') +        end + +        # Foreign key +        if line =~ /t\.integer +"\w+_id"/ && +          !(line =~ /limit: 8/) +          expected_line = line.sub(/(t.integer +"\w+")/, '\1, limit: 8') +        end + +        @original += line +        @expected += expected_line +      end      end -    @non_bigint_primary_keys.empty? && @non_bigint_foreign_keys.empty? +    @original == @expected    end    failure_message do |filename| @@ -35,6 +44,6 @@ RSpec::Matchers.define :use_bigint_keys do    def diff      RSpec::Support::Differ.new(        color: RSpec::Matchers.configuration.color? -    ).diff(@non_bigint_primary_keys + @non_bigint_foreign_keys, []) +    ).diff_as_string(@original, @expected)    end  end | 
