Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Raise when seed finds no files. #99

Merged
merged 2 commits into from
Jan 9, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 15 additions & 2 deletions lib/oaken.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,22 @@ module Stored
singleton_class.attr_reader :lookup_paths
@lookup_paths = ["db/seeds"]

def self.glob(path)
patterns = lookup_paths.map { File.join(_1, "#{path}{,/**/*}.rb") }

Pathname.glob(patterns).tap do |found|
raise NoSeedsFoundError, "found no seed files for #{path.inspect}" if found.none?
end
end
NoSeedsFoundError = Class.new ArgumentError

class Loader
def initialize(path)
@entries = Pathname.glob("#{path}{,/**/*}.rb").sort
def self.from(paths)
new paths.flat_map { Oaken.glob _1 }
end

def initialize(entries)
@entries = entries
end

def load_onto(seeds) = @entries.each do |path|
Expand Down
13 changes: 2 additions & 11 deletions lib/oaken/seeds.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,8 @@ class << self
# class PaginationTest < ActionDispatch::IntegrationTest
# setup { seed "cases/pagination" }
# end
def seed(*directories)
Oaken.lookup_paths.product(directories).each do |path, directory|
load_from Pathname(path).join(directory.to_s)
end
end

private def load_from(path)
@loader = Oaken::Loader.new path
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't need this @loader instance variable, since we're not using it anymore.

@loader.load_onto self
ensure
@loader = nil
def seed(*paths)
Oaken::Loader.from(paths.map(&:to_s)).load_onto self
end

# `section` is purely for decorative purposes to carve up `Oaken.prepare` and seed files.
Expand Down
10 changes: 10 additions & 0 deletions test/dummy/test/models/oaken_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ def self.column_names = []
assert mod.respond_to?(:users) # Now respond_to_missing? hits.
refute mod.respond_to?(:hmhm)
end

test "raises when no files found to seed" do
assert_raise(Oaken::NoSeedsFoundError) { seed "test/cases/missing" }.tap do |error|
assert_match %r|found no seed files for "test/cases/missing"|, error.message
end

assert_raise(Oaken::NoSeedsFoundError) { seed :first_missing, :second_missing }.tap do |error|
assert_match /found no seed files for "first_missing"/, error.message
end
end
end
Loading