Skip to content

Commit

Permalink
Merge pull request #139 from cre-ne-jp/secret-dice-mkdir_p
Browse files Browse the repository at this point in the history
データベースディレクトリを準備するとき、親ディレクトリが存在しなくても再帰的にディレクトリを作成する
  • Loading branch information
koi-chan committed Jul 21, 2019
2 parents ba6d48b + 4269d42 commit 53fd945
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/rgrb/plugin/dice_roll/generator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

require 'gdbm'
require 'json'
require 'fileutils'

module RGRB
module Plugin
Expand All @@ -15,10 +16,15 @@ module DiceRoll
# DiceRoll の出力テキスト生成器
class Generator
include ConfigurableGenerator

# ダイス数が多すぎた場合のメッセージ
# @return [String]
EXCESS_DICE_MESSAGE = "ダイスが机から落ちてしまいましたの☆"

# ジェネレータを初期化する
def initialize
super

@random = Random.new
@mutex_secret_dice = Mutex.new
end
Expand Down Expand Up @@ -127,11 +133,11 @@ def ja_to_i(japanese)
def prepare_db_dir
unless FileTest.exist?(@db_dir)
# 存在しなければ、ディレクトリを作成する
Dir.mkdir(@db_dir)
FileUtils.mkdir_p(@db_dir)
else
unless FileTest.directory?(@db_dir)
# ディレクトリ以外のファイルが存在したらエラー
raise RuntimeError
raise Errno::ENOTDIR, @db_dir
end
end
end
Expand Down
Empty file.
47 changes: 47 additions & 0 deletions spec/rgrb/plugin/dice_roll/generator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
require_relative '../../../spec_helper'
require 'rgrb/plugin/dice_roll/generator'

require 'fileutils'

describe RGRB::Plugin::DiceRoll::Generator do
let(:generator) { described_class.new }

Expand Down Expand Up @@ -127,4 +129,49 @@
end
end
end

describe 'データベースディレクトリの準備' do
let(:data_path_on_test) { File.expand_path('./data', __dir__) }

let(:generator_set_data_path) {
g = generator

g.config_id = 'test'
g.data_path = data_path_on_test
g.configure({ 'JaDice' => true })

g
}

before do
clean_data
end

after do
clean_data
end

it 'ディレクトリが存在しなければ作成する' do
expect(File.directory?("#{data_path_on_test}/test")).to be(false)

generator_set_data_path

expect(File.directory?("#{data_path_on_test}/test")).to be(true)
end

it 'ディレクトリ以外のファイルが存在したら例外を発生させる' do
expect(File.directory?("#{data_path_on_test}/test")).to be(false)

FileUtils.touch("#{data_path_on_test}/test")

expect { generator_set_data_path }.to raise_error(Errno::ENOTDIR)
end

private

# テスト用のデータディレクトリ内のファイルを削除する
def clean_data
FileUtils.rm_rf(Dir.glob("#{data_path_on_test}/*"))
end
end
end

0 comments on commit 53fd945

Please sign in to comment.