Skip to content

Commit

Permalink
設定ファイルのバリデーションエラーが起きた時も設定ファイルのロールバックをする
Browse files Browse the repository at this point in the history
  • Loading branch information
jiikko committed Jan 7, 2024
1 parent 91f870d commit 75fd226
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 29 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.load(setting_path: )
if validator.valid?
[new_instance, yaml]
else
# TODO: fallbackが必要では?
fallback_setting_if_has_backup(current_setting_path: setting_path)
raise ProconBypassMan::CouldNotLoadConfigError, validator.errors_to_s
end
rescue SyntaxError
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,8 @@ def validate_verify_button_existence
flip_option_target_button
}
}.compact.each { |b| unverified_buttons << b }
unless(nunsupport_buttons = (unverified_buttons - ProconBypassMan::Procon::ButtonCollection::BUTTONS)).length.zero?
@errors[:layers] << "#{layer_key}で存在しないボタン#{nunsupport_buttons.join(", ")}があります"
unless(unsupported_buttons = (unverified_buttons - ProconBypassMan::Procon::ButtonCollection::BUTTONS)).length.zero?
@errors[:layers] << "#{layer_key}で存在しないボタン#{unsupported_buttons.join(", ")}があります"
end
end
end
Expand Down
103 changes: 77 additions & 26 deletions spec/lib/procon_bypass_man/buttons_setting_configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,7 @@ def self.binaries; ['a']; end
end
end

context '初回は成功して、次に設定ファイルをロードするとき' do
context '初回のロードには成功して、次の設定ファイルをロードに失敗するとき' do
let(:setting_content) {
<<~EOH
version: 1.0
Expand All @@ -972,14 +972,25 @@ def self.binaries; ['a']; end
set_neutral_position 1000, 1000
EOH
}
let(:error_setting_content) {
let(:syntax_error_setting_content) {
<<~EOH
version: 1.0
setting: |-
prefix_keys_for_changing_layer [:zr, :r, :zl, :l]
prefix_keys_for_changing_layer [:zr]
set_neutral_position 1000,, 1000
EOH
}
let(:validation_error_setting_content) {
<<~EOH
version: 1.0
setting: |-
prefix_keys_for_changing_layer [:zr]
layer :up do
flip :a
remap :a, to: :b
end
EOH
}

before do
valid_setting = Setting.new(setting_content).to_file
Expand All @@ -990,39 +1001,79 @@ def self.binaries; ['a']; end

context 'fallback_pathがある' do
before { File.write(ProconBypassMan.fallback_setting_path, setting_content) }
it 'invalid_setting_contentの内容はファイルに保存されないこと' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(setting_content)
expect(File.exist?(ProconBypassMan.fallback_setting_path)).to eq(false)

context 'バリデーションエラーが起きるとき' do
let(:error_setting_content) { validation_error_setting_content }

it 'invalid_setting_contentの内容はファイルに残らないこと' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(setting_content)
expect(File.exist?(ProconBypassMan.fallback_setting_path)).to eq(false)
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq(previous_configuration.prefix_keys)
expect(ProconBypassMan.buttons_setting_configuration.setting_path).to eq(previous_configuration.setting_path)
end
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
begin
ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path)
rescue ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq([:zr, :r, :zl, :l])
context 'シンタックスエラーが起きるとき' do
let(:error_setting_content) { syntax_error_setting_content }

it 'invalid_setting_contentの内容はファイルに残らないこと' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(setting_content)
expect(File.exist?(ProconBypassMan.fallback_setting_path)).to eq(false)
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq(previous_configuration.prefix_keys)
expect(ProconBypassMan.buttons_setting_configuration.setting_path).to eq(previous_configuration.setting_path)
end
end
end

context 'fallback_pathがない' do
it 'invalid_setting_contentの内容はファイルに保存されること' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(error_setting_content)
context 'バリデーションエラーが起きるとき' do
let(:error_setting_content) { validation_error_setting_content }

it 'invalid_setting_contentの内容はファイルに残ること' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(error_setting_content)
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq(previous_configuration.prefix_keys)
expect(ProconBypassMan.buttons_setting_configuration.setting_path).to eq(previous_configuration.setting_path)
end
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
begin
ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path)
rescue ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq([:zr, :r, :zl, :l])
context 'シンタックスエラーが起きるとき' do
let(:error_setting_content) { syntax_error_setting_content }

it 'invalid_setting_contentの内容はファイルに保存されること' do
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(File.read(invalid_setting.path)).to eq(error_setting_content)
end

it '変更はオブジェクトには反映されないこと' do
previous_configuration = ProconBypassMan.buttons_setting_configuration.dup
invalid_setting = Setting.new(error_setting_content).to_file
expect { ProconBypassMan::ButtonsSettingConfiguration::Loader.load(setting_path: invalid_setting.path) }.to raise_error ProconBypassMan::CouldNotLoadConfigError
expect(ProconBypassMan.buttons_setting_configuration.prefix_keys).to eq(previous_configuration.prefix_keys)
expect(ProconBypassMan.buttons_setting_configuration.setting_path).to eq(previous_configuration.setting_path)
end
end
Expand Down

0 comments on commit 75fd226

Please sign in to comment.