-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
102 additions
and
176 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,129 +1,92 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe Dsu::Migration::Service20230613121411 do | ||
RSpec.describe Dsu::Migration::Service20230613121411, type: :migration do | ||
subject(:service) { described_class.new(options: options) } | ||
|
||
shared_examples 'the migration is successful' do | ||
it 'backs up the old config file' do | ||
expect(File.exist?(File.join(backup_folder, Dsu::Support::Fileable.config_file_name))).to be true | ||
end | ||
|
||
it 'backs up the old entry files' do | ||
entries_folder = File.basename(Dsu::Support::Fileable.entries_folder) | ||
expect(File.exist?(File.join(backup_folder, entries_folder, Dsu::Support::Fileable.entries_file_name(time: time)))).to be true | ||
end | ||
|
||
it 'backs up the old theme files' do | ||
themes_folder = File.basename(Dsu::Support::Fileable.themes_folder) | ||
expect(File.exist?(File.join(backup_folder, themes_folder, Dsu::Support::Fileable.theme_file_name(theme_name: theme_name)))).to be true | ||
end | ||
|
||
it 'creates the new config file' do | ||
expect(File.exist?(Dsu::Support::Fileable.config_path)).to be true | ||
end | ||
|
||
it 'copies the new theme files' do | ||
theme_names = %w[cherry default lemon matrix whiteout] | ||
themes_exist = theme_names.all? { |theme_name| File.exist?(Dsu::Support::Fileable.themes_path(theme_name: theme_name)) } | ||
expect(themes_exist).to be true | ||
end | ||
|
||
it 'creates an initial entry group' do | ||
expect(File.exist?(Dsu::Support::Fileable.entries_path(time: time))).to be true | ||
end | ||
before do | ||
create(:migration_version, version: migration_version, options: options) | ||
end | ||
|
||
let(:dsu_folder) { File.join(temp_folder, 'dsu') } | ||
|
||
let(:options) { {} } | ||
let(:migration_version) { 0 } | ||
|
||
describe 'class methods' do | ||
describe '.run_migrations?' do | ||
context 'when the migration version is current' do | ||
before do | ||
create(:migration_version, :with_current_version) | ||
end | ||
describe '#initialize' do | ||
let(:migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals | ||
|
||
it 'returns false' do | ||
expect(described_class.run_migrations?).to be(false) | ||
end | ||
end | ||
it_behaves_like 'no error is raised' | ||
end | ||
|
||
context 'when the migration version less than 20230613121411' do | ||
before do | ||
create(:migration_version, version: 20230613121411 - 1) # rubocop:disable Style/NumericLiterals | ||
end | ||
describe '#migrate_if!' do | ||
subject(:service_migrate_if) { service.migrate_if! } | ||
|
||
it 'returns true' do | ||
expect(described_class.run_migrations?).to be(true) | ||
end | ||
context 'when the migration version is not 0' do | ||
before do | ||
mock_migration_version_for(version: migration_version) | ||
service_migrate_if | ||
end | ||
|
||
context 'when the migration version greater than the current version' do | ||
before do | ||
create(:migration_version, version: Dsu::Migration::VERSION + 1) | ||
end | ||
let(:migration_version) { 20240210161248 } # rubocop:disable Style/NumericLiterals | ||
let(:expected) { File.join('spec', 'fixtures', 'folders', migration_version.to_s) } | ||
let(:actual) { dsu_folder } | ||
|
||
it 'returns false' do | ||
expect(described_class.run_migrations?).to be(false) | ||
end | ||
it 'does not make any changes to the dsu folder structure or configuration file' do | ||
expect(dsu_folders_and_file_contents_match?(expected: expected, actual: actual)).to be(true) | ||
end | ||
end | ||
end | ||
|
||
context 'when migrations should not be run' do | ||
subject(:service) { build(:migration_service) } | ||
|
||
before do | ||
create(:migration_version, :with_current_version) | ||
end | ||
|
||
specify 'the migration version file exists' do | ||
migration_version = create(:migration_version, :with_current_version) | ||
expect(migration_version).to exist | ||
end | ||
|
||
it 'does not run migrations' do | ||
expect { service.call }.to output(/Nothing to do/).to_stdout | ||
end | ||
end | ||
|
||
context 'when migrations should be run' do | ||
subject(:service) { build(:migration_service) } | ||
|
||
before do | ||
FileUtils.touch(Dsu::Support::Fileable.config_path) | ||
FileUtils.touch(Dsu::Support::Fileable.entries_path(time: time)) | ||
FileUtils.touch(Dsu::Support::Fileable.themes_path(theme_name: theme_name)) | ||
end | ||
context 'when the migration version is 0' do | ||
before do | ||
mock_migration_version_for(version: migration_version) | ||
service_migrate_if | ||
end | ||
|
||
let(:time) { Time.now.in_time_zone } | ||
let(:theme_name) { 'old_theme' } | ||
let(:backup_folder) { Dsu::Support::Fileable.backup_folder(version: 0) } | ||
shared_examples 'the migration was run in pretend mode' do | ||
let(:expected) { File.join('spec', 'fixtures', 'folders', migration_version.to_s) } | ||
let(:actual) { dsu_folder } | ||
|
||
context 'when the migration file exists' do | ||
before do | ||
service.call | ||
it 'does not make any changes to the dsu folder structure or configuration file' do | ||
expect(dsu_folders_and_file_contents_match?(expected: expected, actual: actual)).to be(true) | ||
end | ||
end | ||
|
||
specify 'the migration version file exists' do | ||
migration_version = build(:migration_version, version: 0) | ||
expect(migration_version).to exist | ||
context 'when the pretend option is implicitly set to true (not provided)' do | ||
it_behaves_like 'the migration was run in pretend mode' | ||
end | ||
|
||
it_behaves_like 'the migration is successful' | ||
end | ||
context 'when the pretend option is explicitly set to true' do | ||
let(:options) { { pretend: true } } | ||
|
||
context 'when the migration file does not exist' do | ||
before do | ||
service.call | ||
it_behaves_like 'the migration was run in pretend mode' | ||
end | ||
|
||
specify 'the migration version file does not exist' do | ||
migration_version = build(:migration_version) | ||
migration_version.delete | ||
expect(migration_version).to_not exist | ||
end | ||
context 'when the pretend option is set to false' do | ||
let(:options) { { pretend: false } } | ||
let(:expected) { File.join('spec', 'fixtures', 'folders', 20230613121411.to_s) } # rubocop:disable Style/NumericLiterals | ||
let(:actual) { dsu_folder } | ||
|
||
it_behaves_like 'the migration is successful' | ||
it 'migrates the dsu folder structure and files' do | ||
expect(dsu_folders_and_file_contents_match?(expected: expected, actual: actual, | ||
known_deleted_files: known_deleted_files, known_added_files: known_added_files)).to be(true) | ||
end | ||
end | ||
end | ||
end | ||
|
||
def known_deleted_files | ||
%w[ | ||
entries/2023-12-28.json | ||
entries/2023-12-29.json | ||
entries/2024-01-01.json | ||
entries/2024-01-02.json | ||
] | ||
end | ||
|
||
def known_added_files | ||
%w[ | ||
themes/christmas.json | ||
themes/light.json | ||
] | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.