From cad394bf9d754c85833cf79806a50ab29a9dcdab Mon Sep 17 00:00:00 2001 From: Weston Ganger Date: Sat, 14 Oct 2023 12:47:25 -0700 Subject: [PATCH] Fix cleaning of old tmp files created by export_to --- CHANGELOG.md | 1 + app/models/rails_i18n_manager/translation_key.rb | 10 ++++------ spec/unit/models/translation_key_spec.rb | 14 +++++++++++++- 3 files changed, 18 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 31c9501..6a95cde 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/app/models/rails_i18n_manager/translation_key.rb b/app/models/rails_i18n_manager/translation_key.rb index 432f7ee..6334b26 100644 --- a/app/models/rails_i18n_manager/translation_key.rb +++ b/app/models/rails_i18n_manager/translation_key.rb @@ -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) diff --git a/spec/unit/models/translation_key_spec.rb b/spec/unit/models/translation_key_spec.rb index bea3da5..4b47fb9 100644 --- a/spec/unit/models/translation_key_spec.rb +++ b/spec/unit/models/translation_key_spec.rb @@ -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 @@ -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")