|
1 | | -require 'spec_helper' |
| 1 | +require 'mini_i18n' |
2 | 2 | require 'mini_i18n/cli' |
3 | 3 | require 'csv' |
4 | 4 | require 'tempfile' |
| 5 | +require 'fileutils' |
| 6 | +require 'yaml' |
| 7 | +require 'stringio' |
5 | 8 |
|
6 | 9 | RSpec.describe MiniI18n::CLI do |
7 | 10 | let(:cli) { described_class.new(args) } |
|
35 | 38 | let(:args) { ['unknown'] } |
36 | 39 |
|
37 | 40 | it 'prints error and exits' do |
38 | | - expect { cli.run }.to output(/Unknown command: unknown/).to_stdout |
39 | | - expect { cli.run }.to raise_error(SystemExit) |
| 41 | + expect { |
| 42 | + begin |
| 43 | + cli.run |
| 44 | + rescue SystemExit |
| 45 | + # Capture the SystemExit |
| 46 | + end |
| 47 | + }.to output(/Unknown command: unknown/).to_stdout |
40 | 48 | end |
41 | 49 | end |
42 | 50 | end |
43 | 51 |
|
44 | 52 | describe 'with translation files' do |
45 | 53 | let(:temp_dir) { Dir.mktmpdir } |
| 54 | + let(:original_dir) { Dir.pwd } |
46 | 55 |
|
47 | 56 | before do |
| 57 | + # Change to temp directory so CLI can find files |
| 58 | + Dir.chdir(temp_dir) |
| 59 | + |
| 60 | + # Create locales directory |
| 61 | + FileUtils.mkdir_p('locales') |
| 62 | + |
48 | 63 | # Create test translation files |
49 | 64 | en_content = { |
50 | 65 | 'en' => { |
|
60 | 75 | } |
61 | 76 | } |
62 | 77 |
|
63 | | - File.write(File.join(temp_dir, 'en.yml'), en_content.to_yaml) |
64 | | - File.write(File.join(temp_dir, 'es.yml'), es_content.to_yaml) |
65 | | - |
66 | | - # Mock the glob pattern to find our test files |
67 | | - allow(Dir).to receive(:glob).and_call_original |
68 | | - allow(Dir).to receive(:glob).with('config/locales/*.yml').and_return([]) |
69 | | - allow(Dir).to receive(:glob).with('config/locales/*.yaml').and_return([]) |
70 | | - allow(Dir).to receive(:glob).with('locales/*.yml').and_return([ |
71 | | - File.join(temp_dir, 'en.yml'), |
72 | | - File.join(temp_dir, 'es.yml') |
73 | | - ]) |
| 78 | + File.write('locales/en.yml', en_content.to_yaml) |
| 79 | + File.write('locales/es.yml', es_content.to_yaml) |
74 | 80 | end |
75 | 81 |
|
76 | 82 | after do |
| 83 | + Dir.chdir(original_dir) |
77 | 84 | FileUtils.rm_rf(temp_dir) |
78 | 85 | end |
79 | 86 |
|
|
115 | 122 | end |
116 | 123 |
|
117 | 124 | context 'with export command' do |
118 | | - let(:temp_csv) { Tempfile.new(['test', '.csv']) } |
119 | | - let(:args) { ['export', "--file=#{temp_csv.path}"] } |
120 | | - |
121 | | - after do |
122 | | - temp_csv.close |
123 | | - temp_csv.unlink |
124 | | - end |
| 125 | + let(:temp_csv) { File.join(temp_dir, 'test_export.csv') } |
| 126 | + let(:args) { ['export', "--file=#{temp_csv}"] } |
125 | 127 |
|
126 | 128 | it 'exports translations to CSV' do |
127 | 129 | output = capture_stdout { cli.run } |
128 | 130 |
|
129 | | - expect(output).to include("Translations exported successfully to #{temp_csv.path}") |
| 131 | + expect(output).to include("Translations exported successfully to #{temp_csv}") |
| 132 | + expect(File.exist?(temp_csv)).to be true |
130 | 133 |
|
131 | | - csv_content = CSV.read(temp_csv.path, headers: true) |
| 134 | + csv_content = CSV.read(temp_csv, headers: true) |
132 | 135 | expect(csv_content.headers).to eq(['key', 'en', 'es']) |
133 | 136 | expect(csv_content.map(&:to_h)).to include( |
134 | 137 | { 'key' => 'hello', 'en' => 'Hello', 'es' => 'Hola' } |
135 | 138 | ) |
136 | 139 | end |
137 | 140 | end |
| 141 | + |
| 142 | + context 'with import command' do |
| 143 | + let(:temp_csv) { File.join(temp_dir, 'test_import.csv') } |
| 144 | + let(:args) { ['import', "--file=#{temp_csv}"] } |
| 145 | + |
| 146 | + before do |
| 147 | + # Create a CSV file to import |
| 148 | + CSV.open(temp_csv, 'w') do |csv| |
| 149 | + csv << ['key', 'en', 'es'] |
| 150 | + csv << ['hello', 'Hello', 'Hola'] |
| 151 | + csv << ['goodbye', 'Goodbye', 'Adiós'] |
| 152 | + end |
| 153 | + end |
| 154 | + |
| 155 | + it 'imports translations from CSV' do |
| 156 | + output = capture_stdout { cli.run } |
| 157 | + |
| 158 | + expect(output).to include("Translations imported successfully from #{temp_csv}") |
| 159 | + end |
| 160 | + end |
138 | 161 | end |
139 | 162 |
|
140 | 163 | private |
|
0 commit comments