debian-mirror-gitlab/spec/tasks/gettext_rake_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

142 lines
4.4 KiB
Ruby
Raw Normal View History

2021-02-22 17:27:13 +05:30
# frozen_string_literal: true
2021-09-04 01:27:46 +05:30
require 'rake_helper'
2023-06-20 00:43:36 +05:30
require_relative '../../tooling/lib/tooling/gettext_extractor'
require_relative '../support/matchers/abort_matcher'
2021-02-22 17:27:13 +05:30
2023-06-20 00:43:36 +05:30
RSpec.describe 'gettext', :silence_stdout, feature_category: :internationalization do
2021-02-22 17:27:13 +05:30
let(:locale_path) { Rails.root.join('tmp/gettext_spec') }
let(:pot_file_path) { File.join(locale_path, 'gitlab.pot') }
before do
Rake.application.rake_require('tasks/gettext')
FileUtils.rm_r(locale_path) if Dir.exist?(locale_path)
FileUtils.mkdir_p(locale_path)
allow(Rails.root).to receive(:join).and_call_original
allow(Rails.root).to receive(:join).with('locale').and_return(locale_path)
end
after do
FileUtils.rm_r(locale_path) if Dir.exist?(locale_path)
end
describe ':compile' do
2023-06-20 00:43:36 +05:30
let(:compile_command) do
[
"node", "./scripts/frontend/po_to_json.js",
"--locale-root", Rails.root.join('locale').to_s,
"--output-dir", Rails.root.join('app/assets/javascripts/locale').to_s
]
2021-02-22 17:27:13 +05:30
end
2023-06-20 00:43:36 +05:30
it 'creates a pot file and runs po-to-json conversion via node script' do
expect(Kernel).to receive(:system).with(*compile_command).and_return(true)
2021-02-22 17:27:13 +05:30
expect { run_rake_task('gettext:compile') }
.to change { File.exist?(pot_file_path) }
.to be_truthy
end
2023-06-20 00:43:36 +05:30
it 'aborts with non-successful po-to-json conversion via node script' do
expect(Kernel).to receive(:system).with(*compile_command).and_return(false)
expect { run_rake_task('gettext:compile') }.to abort_execution
end
2021-02-22 17:27:13 +05:30
end
describe ':regenerate' do
2023-06-20 00:43:36 +05:30
let(:locale_nz_path) { File.join(locale_path, 'en_NZ') }
let(:po_file_path) { File.join(locale_nz_path, 'gitlab.po') }
let(:extractor) { instance_double(Tooling::GettextExtractor, generate_pot: '') }
2021-02-22 17:27:13 +05:30
before do
2023-06-20 00:43:36 +05:30
FileUtils.mkdir(locale_nz_path)
File.write(po_file_path, fixture_file('valid.po'))
2021-02-22 17:27:13 +05:30
# this task takes a *really* long time to complete, so stub it for the spec
2023-06-20 00:43:36 +05:30
allow(Tooling::GettextExtractor).to receive(:new).and_return(extractor)
2021-02-22 17:27:13 +05:30
end
context 'when the locale folder is not found' do
before do
FileUtils.rm_r(locale_path) if Dir.exist?(locale_path)
end
it 'raises an error' do
expect { run_rake_task('gettext:regenerate') }
.to raise_error(/Cannot find '#{locale_path}' folder/)
end
end
context 'when the gitlab.pot file cannot be generated' do
it 'prints an error' do
2023-06-20 00:43:36 +05:30
allow(File).to receive(:exist?).and_return(false)
2021-02-22 17:27:13 +05:30
expect { run_rake_task('gettext:regenerate') }
.to raise_error(/gitlab.pot file not generated/)
end
end
end
describe ':lint' do
before do
# make sure we test on the fixture files, not the actual gitlab repo as
# this takes a long time
allow(Rails.root)
.to receive(:join)
.with('locale/*/gitlab.po')
.and_return(File.join(locale_path, '*/gitlab.po'))
end
context 'when all PO files are valid' do
before do
nz_locale_path = File.join(locale_path, 'en_NZ')
FileUtils.mkdir(nz_locale_path)
po_file_path = File.join(nz_locale_path, 'gitlab.po')
File.write(po_file_path, fixture_file('valid.po'))
File.write(pot_file_path, fixture_file('valid.po'))
end
it 'completes without error' do
expect { run_rake_task('gettext:lint') }
.not_to raise_error
end
end
context 'when there are invalid PO files' do
before do
nz_locale_path = File.join(locale_path, 'en_NZ')
FileUtils.mkdir(nz_locale_path)
po_file_path = File.join(nz_locale_path, 'gitlab.po')
File.write(po_file_path, fixture_file('invalid.po'))
File.write(pot_file_path, fixture_file('valid.po'))
end
it 'raises an error' do
expect { run_rake_task('gettext:lint') }
.to raise_error(/Not all PO-files are valid/)
end
end
context 'when the .pot file is invalid' do
before do
nz_locale_path = File.join(locale_path, 'en_NZ')
FileUtils.mkdir(nz_locale_path)
po_file_path = File.join(nz_locale_path, 'gitlab.po')
File.write(po_file_path, fixture_file('valid.po'))
File.write(pot_file_path, fixture_file('invalid.po'))
end
it 'raises an error' do
expect { run_rake_task('gettext:lint') }
.to raise_error(/Not all PO-files are valid/)
end
end
end
end