Skip to content

Commit

Permalink
Fix cleaning of old tmp files created by export_to
Browse files Browse the repository at this point in the history
  • Loading branch information
westonganger committed Oct 18, 2023
1 parent 74bfc7c commit 0e288b1
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

### Unreleased - [View Diff](https://github.com/westonganger/rails_i18n_manager/compare/v1.0.0...master)
- [#13](https://github.com/westonganger/rails_i18n_manager/pull/13) - Remove usage of Array#intersection to fix errors in Ruby 2.6 and below
- [#12](https://github.com/westonganger/rails_i18n_manager/pull/12) - Fix for cleaning old tmp files created from TranslationKey#export_to
- [#11](https://github.com/westonganger/rails_i18n_manager/pull/11) - Fix google translate and add specs
- [#10](https://github.com/westonganger/rails_i18n_manager/pull/10) - Add missing pagination links to index pages

Expand Down
10 changes: 4 additions & 6 deletions app/models/rails_i18n_manager/translation_key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ def self.export_to(app_name: nil, zip: false, format: :yaml)

base_export_path = Rails.root.join("tmp/export/translations/")

files_to_delete = Dir.glob("#{base_export_path}/*").each do |f|
if File.ctime(f) > 1.minutes.ago
`rm -rf #{f}`
#File.delete(f)
end
files_to_delete = Dir.glob("#{base_export_path}/*").select{|f| File.ctime(f) < 1.minutes.ago }
if !files_to_delete.empty?
FileUtils.rm_r(files_to_delete, force: true)
end

base_folder_path = File.join(base_export_path, "#{Time.now.to_i}/")
base_folder_path = File.join(base_export_path, "#{Time.now.to_i}-#{SecureRandom.hex(6)}/")

FileUtils.mkdir_p(base_folder_path)

Expand Down
14 changes: 13 additions & 1 deletion spec/unit/models/translation_key_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ module RailsI18nManager
dirname = TranslationKey.export_to(app_name: nil, zip: false, format: "yaml")
expect(File.directory?(dirname)).to eq(true)
app_dirs = Dir.glob("#{dirname}/*")
expect(app_dirs.size).to eq(2)
expect(app_dirs.size > 1)
expect(app_dirs.any?{|x| File.directory?(x) && x.end_with?("/#{TranslationApp.first.name}")}).to eq(true)
expect(app_dirs.any?{|x| File.directory?(x) && x.end_with?("/#{TranslationApp.last.name}")}).to eq(true)
end
Expand Down Expand Up @@ -106,6 +106,18 @@ module RailsI18nManager
expect(files[1].end_with?("/fr.yml")).to eq(true)
end

it "deletes old tmp files" do
allow(FileUtils).to receive(:rm_r).and_call_original

TranslationKey.export_to(app_name: nil, zip: false, format: "yaml")
TranslationKey.export_to(app_name: nil, zip: false, format: "yaml")
expect(FileUtils).not_to have_received(:rm_r)

allow(File).to receive(:ctime).and_return(2.minutes.ago)
TranslationKey.export_to(app_name: nil, zip: false, format: "yaml")
expect(FileUtils).to have_received(:rm_r).once
end

context "yaml" do
it "outputs content in yaml" do
dirname = TranslationKey.export_to(app_name: nil, zip: false, format: "yaml")
Expand Down

0 comments on commit 0e288b1

Please sign in to comment.