diff options
author | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-21 11:42:44 -0300 |
---|---|---|
committer | Douglas Barbosa Alexandre <dbalexandre@gmail.com> | 2016-07-21 11:47:28 -0300 |
commit | 2053d89172b5edfa56c7f4e0451fcff74a5e26b5 (patch) | |
tree | aba2606330a9942485987d27f3fe97bb54ae189f /spec | |
parent | 92ee8c5e6489a91d672f51a8807d755e52db4c05 (diff) | |
download | gitlab-ce-2053d89172b5edfa56c7f4e0451fcff74a5e26b5.tar.gz |
Extract helper methods to clean up RepositoryArchiveCleanUpService spec
Diffstat (limited to 'spec')
-rw-r--r-- | spec/services/repository_archive_clean_up_service_spec.rb | 103 |
1 files changed, 37 insertions, 66 deletions
diff --git a/spec/services/repository_archive_clean_up_service_spec.rb b/spec/services/repository_archive_clean_up_service_spec.rb index 0a3c262df32..842585f9e54 100644 --- a/spec/services/repository_archive_clean_up_service_spec.rb +++ b/spec/services/repository_archive_clean_up_service_spec.rb @@ -18,93 +18,64 @@ describe RepositoryArchiveCleanUpService, services: true do end context 'when the downloads directory exists' do - context 'when archives older than 2 hours exists' do - it 'removes old files that matches valid archive extensions' do - Dir.mktmpdir do |path| - stub_repository_downloads_path(path) - dirname = File.join(path, 'sample.git') - files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip], 2.hours) - - service.execute - - files.each { |file| expect(File.exist?(file)).to eq false } - expect(File.directory?(dirname)).to eq false - end - end - - it 'keeps old files that does not matches valid archive extensions' do - Dir.mktmpdir do |path| - stub_repository_downloads_path(path) - dirname = File.join(path, 'sample.git') - files = create_temporary_files(dirname, %w[conf rb], 2.hours) - - service.execute - - files.each { |file| expect(File.exist?(file)).to eq true } - expect(File.directory?(dirname)).to eq true - end - end - - it 'keeps old files inside invalid directories' do - Dir.mktmpdir do |path| - stub_repository_downloads_path(path) - dirname = File.join(path, 'john_doe/sample.git') - files = create_temporary_files(dirname, %w[conf rb tar tar.gz], 2.hours) - + shared_examples 'invalid archive files' do |dirname, extensions, mtime| + it 'does not remove files and directoy' do + in_directory_with_files(dirname, extensions, mtime) do |dir, files| service.execute files.each { |file| expect(File.exist?(file)).to eq true } - expect(File.directory?(dirname)).to eq true + expect(File.directory?(dir)).to eq true end end end - context 'when archives older than 2 hours does not exist' do - it 'keeps files that matches valid archive extensions' do - Dir.mktmpdir do |path| - dirname = File.join(path, 'sample.git') - files = create_temporary_files(dirname, %w[tar tar.bz2 tar.gz zip], 1.hour) + it 'removes files older than 2 hours that matches valid archive extensions' do + in_directory_with_files('sample.git', %w[tar tar.bz2 tar.gz zip], 2.hours) do |dir, files| + service.execute - service.execute - - files.each { |file| expect(File.exist?(file)).to eq true } - expect(File.directory?(dirname)).to eq true - end + files.each { |file| expect(File.exist?(file)).to eq false } + expect(File.directory?(dir)).to eq false end + end - it 'keeps files that does not matches valid archive extensions' do - Dir.mktmpdir do |path| - dirname = File.join(path, 'sample.git') - files = create_temporary_files(dirname, %w[conf rb], 1.hour) - - service.execute + context 'with files older than 2 hours that does not matches valid archive extensions' do + it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb], 2.hours + end - files.each { |file| expect(File.exist?(file)).to eq true } - expect(File.directory?(dirname)).to eq true - end - end + context 'with files older than 2 hours inside invalid directories' do + it_behaves_like 'invalid archive files', 'john_doe/sample.git', %w[conf rb tar tar.gz], 2.hours + end - it 'keeps files inside invalid directories' do - Dir.mktmpdir do |path| - dirname = File.join(path, 'john_doe/sample.git') - files = create_temporary_files(dirname, %w[conf rb tar tar.gz], 1.hour) + context 'with files newer than 2 hours that matches valid archive extensions' do + it_behaves_like 'invalid archive files', 'sample.git', %w[tar tar.bz2 tar.gz zip], 1.hour + end - service.execute + context 'with files newer than 2 hours that does not matches valid archive extensions' do + it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb], 1.hour + end - files.each { |file| expect(File.exist?(file)).to eq true } - expect(File.directory?(dirname)).to eq true - end - end + context 'with files newer than 2 hours inside invalid directories' do + it_behaves_like 'invalid archive files', 'sample.git', %w[conf rb tar tar.gz], 1.hour end end - def create_temporary_files(dirname, extensions, mtime) - FileUtils.mkdir_p(dirname) - FileUtils.touch(extensions.map { |ext| File.join(dirname, "sample.#{ext}") }, mtime: Time.now - mtime) + def in_directory_with_files(dirname, extensions, mtime) + Dir.mktmpdir do |tmpdir| + stub_repository_downloads_path(tmpdir) + dir = File.join(tmpdir, dirname) + files = create_temporary_files(dir, extensions, mtime) + + yield(dir, files) + end end def stub_repository_downloads_path(path) allow(Gitlab.config.gitlab).to receive(:repository_downloads_path).and_return(path) end + + def create_temporary_files(dir, extensions, mtime) + FileUtils.mkdir_p(dir) + FileUtils.touch(extensions.map { |ext| File.join(dir, "sample.#{ext}") }, mtime: Time.now - mtime) + end end end |