File: gettext_rake_spec.rb

package info (click to toggle)
ruby-gettext-setup 0.34-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 292 kB
  • sloc: ruby: 807; makefile: 5
file content (134 lines) | stat: -rw-r--r-- 4,744 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# frozen_string_literal: true

require 'rspec/expectations'
require 'rake'
require 'spec_helper'

begin
  load File.expand_path('../../../../lib/tasks/gettext.rake', __FILE__)
rescue LoadError
  load '/usr/lib/ruby/vendor_ruby/tasks/gettext.rake'
end

describe 'gettext.rake' do
  locales = File.expand_path('../../fixtures/locales', File.dirname(__FILE__))
  tmp_locales = File.expand_path('../../fixtures/tmp_locales', File.dirname(__FILE__))
  fixture_locales = File.expand_path('../../fixtures/fixture_locales', File.dirname(__FILE__))
  tmp_pot_path = File.expand_path('sinatra-i18n.pot', tmp_locales)
  merge_locales = File.expand_path('../../fixtures/merge_locales', File.dirname(__FILE__))

  before :each do
    FileUtils.rm_r(tmp_locales, force: true)
    FileUtils.cp_r(locales, tmp_locales)
  end
  after :each do
    GettextSetup.clear
    Rake::Task.tasks.each(&:reenable)
  end
  around :each do |test|
    # Since we have `exit 1` in these rake tasks, we need to explicitly tell
    # rspec that any unexpected errors aren't expected. Otherwise, if a
    # SystemExit error is thrown, it just doesn't finish running the rest of
    # the tests and considers the suite passing...
    expect { test.run }.not_to raise_error
  end
  context Rake::Task['gettext:pot'] do
    it 'outputs correctly' do
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to output(/POT file .+\/spec\/fixtures\/tmp_locales\/sinatra-i18n.pot has been generated/).to_stdout
    end
    it 'exits 1 on error' do
      allow(GettextSetup::Pot).to receive(:generate_new_pot).and_return(false)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to raise_error(SystemExit)
    end
  end
  context Rake::Task['gettext:pot'] do
    it 'outputs correctly, when passing a filename' do
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke(File.expand_path('bill.pot', tmp_locales))
      end.to output(/POT file .+\/spec\/fixtures\/tmp_locales\/bill.pot has been generated/).to_stdout
    end
  end
  context Rake::Task['gettext:metadata_pot'] do
    it 'outputs correctly' do
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to output(/POT metadata file .+sinatra-i18n_metadata.pot has been generated/).to_stdout
    end
    it 'exits 1 on error' do
      allow(GettextSetup::MetadataPot).to receive(:generate_metadata_pot).and_return(false)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to raise_error(SystemExit)
    end
  end
  context Rake::Task['gettext:po'] do
    it 'outputs correctly' do
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke('de')
      end.to output(/PO file .+de\/sinatra-i18n.po merged/).to_stdout
    end
    it 'exits 1 on error' do
      allow(GettextSetup::Pot).to receive(:generate_new_po).with('de').and_return(false)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke('de')
      end.to raise_error(SystemExit)
    end
  end
  context Rake::Task['gettext:update_pot'] do
    it 'does not update the POT when no changes are detected' do
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to output(/No string changes detected, keeping old POT file/).to_stdout
    end
    it 'can create a new POT' do
      FileUtils.rm(tmp_pot_path)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to output(/No existing POT file, generating new\nPOT file .+sinatra-i18n.pot has been generated/).to_stdout
    end
    it 'can update the POT' do
      fixture_locales_pot = File.expand_path('fixture_locales.pot', fixture_locales)
      FileUtils.cp(fixture_locales_pot, tmp_pot_path)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to output(/String changes detected, replacing with updated POT file/).to_stdout
    end
    it 'exits 1 upon error' do
      allow(GettextSetup::Pot).to receive(:update_pot).and_return(false)
      expect do
        GettextSetup.initialize(tmp_locales)
        subject.invoke
      end.to raise_error(SystemExit)
    end
  end

  context Rake::Task['gettext:merge'] do
    it 'outputs correctly' do
      expect do
        GettextSetup.initialize(merge_locales)
        subject.invoke
      end.to output(/PO files have been successfully merged/).to_stdout
    end
    it 'exits 1 on error' do
      allow(GettextSetup::Pot).to receive(:merge).and_return(false)
      expect do
        GettextSetup.initialize(merge_locales)
        subject.invoke
      end.to raise_error(SystemExit)
    end
  end
end