-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add seed command * Fix findings
- Loading branch information
Showing
7 changed files
with
148 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
require "./spec_helper" | ||
|
||
describe Marten::CLI::Manage::Command::Seed do | ||
after_each do | ||
File.delete("./seed.cr") if File.exists?("./seed.cr") | ||
end | ||
|
||
describe "#run" do | ||
it "runs the default seed file when no custom path is provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
# Create a mock seed file to simulate seeding | ||
message = "Seeding..." | ||
File.write("./seed.cr", "puts \"#{message}\"") | ||
|
||
command = Marten::CLI::Manage::Command::Seed.new( | ||
options: [] of String, | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
command.handle | ||
output = stdout.rewind.gets_to_end | ||
|
||
output.includes?("Running seed file at ./seed.cr").should be_true | ||
output.includes?(message).should be_true | ||
stderr.rewind.gets_to_end.empty?.should be_true | ||
end | ||
|
||
it "runs the seed file from a custom path when provided" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
custom_seed_path = "./custom_seed.cr" | ||
message = "Custom seeding..." | ||
File.write(custom_seed_path, "puts \"#{message}\"") | ||
|
||
command = Marten::CLI::Manage::Command::Seed.new( | ||
options: ["--file", custom_seed_path], | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
command.handle | ||
|
||
output = stdout.rewind.gets_to_end | ||
|
||
File.delete(custom_seed_path) if File.exists?(custom_seed_path) | ||
|
||
output.includes?("Running seed file at #{custom_seed_path}").should be_true | ||
output.includes?(message).should be_true | ||
stderr.rewind.gets_to_end.empty?.should be_true | ||
end | ||
|
||
it "prints an error if no seed file was found" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
command = Marten::CLI::Manage::Command::Seed.new( | ||
options: [] of String, | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
command.handle | ||
|
||
stdout.rewind.gets_to_end.includes?("Seed file not found at ./seed.cr").should be_true | ||
end | ||
|
||
it "handles errors if the seed file fails to run" do | ||
stdout = IO::Memory.new | ||
stderr = IO::Memory.new | ||
|
||
# Create a seed file with invalid content | ||
File.write("./seed.cr", "invalid_crystal_code") | ||
|
||
command = Marten::CLI::Manage::Command::Seed.new( | ||
options: [] of String, | ||
stdout: stdout, | ||
stderr: stderr | ||
) | ||
|
||
command.handle | ||
|
||
output = stderr.rewind.gets_to_end | ||
output.includes?("Error:").should be_true | ||
end | ||
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
6 changes: 6 additions & 0 deletions
6
src/marten/cli/manage/command/new/templates/project/seed.cr.ecr
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 |
---|---|---|
@@ -0,0 +1,6 @@ | ||
require "./src/project" | ||
|
||
# Setup the project. | ||
Marten.setup | ||
|
||
# Add your seeding logic here. |
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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
module Marten | ||
module CLI | ||
class Manage | ||
module Command | ||
class Seed < Base | ||
@seed_path : String = "./seed.cr" | ||
|
||
help "Populate the database by running the seed file." | ||
|
||
def setup | ||
on_option_with_arg( | ||
:f, | ||
:file, | ||
arg: "path", | ||
description: "Specify a custom path to the seed file" | ||
) do |v| | ||
@seed_path = v | ||
end | ||
end | ||
|
||
def run | ||
run_seed_file | ||
end | ||
|
||
private getter seed_path | ||
|
||
private def run_seed_file | ||
if File.exists?(seed_path) | ||
print "Running seed file at #{seed_path}" | ||
Process.run("crystal #{seed_path}", shell: true, output: stdout, error: stderr) | ||
else | ||
print "Seed file not found at #{seed_path}." | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |