Skip to content

Commit

Permalink
Raise when seed finds no files. (#99)
Browse files Browse the repository at this point in the history
* Raise when `seed` finds no files.

Ref #95

In case users pass a typo'ed name/path to `seed` we won't give them any indication of what's going on.

Running a test fails silently.

Now we raise when we can't find any files.

* Split glob per path pattern

In case a user does `seed :accounts, :missing` we want to raise when we can't load from `:missing`.

Previously, we'd proceed silently because `:accounts` found files.
  • Loading branch information
kaspth authored Jan 9, 2025
1 parent fafa951 commit eeb2e87
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 13 deletions.
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
@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

0 comments on commit eeb2e87

Please sign in to comment.