Skip to content

Commit

Permalink
Merge pull request #5 from Raushan998/feat/add_database
Browse files Browse the repository at this point in the history
added rspec and setup the workflow to run the tests
  • Loading branch information
Raushan998 authored Jan 13, 2025
2 parents f7c70c8 + d39d634 commit 19fe483
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 65 deletions.
17 changes: 14 additions & 3 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -1,8 +1,19 @@
# .rubocop.yml
AllCops:
TargetRubyVersion: 3.0
Exclude:
- 'vendor/**/*'
- '.bundle/**/*'
- 'vendor/bundle/**/*'
- 'Gemfile'
- 'Gemfile.lock'
- 'bin/*'
- 'exe/*'

Style/Documentation:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes
Enabled: false

Style/StringLiteralsInInterpolation:
EnforcedStyle: double_quotes
Enabled: false
24 changes: 13 additions & 11 deletions lib/rubypit/cli.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
require 'thor'
require 'fileutils'
# frozen_string_literal: true

require "thor"
require "fileutils"

module Rubypit
class CLI < Thor
Expand Down Expand Up @@ -29,28 +31,28 @@ def generate

def create_project_directory
FileUtils.mkdir_p(project_path)
FileUtils.mkdir_p(File.join(project_path, 'app'))
FileUtils.mkdir_p(File.join(project_path, 'app', 'models'))
FileUtils.mkdir_p(File.join(project_path, "app"))
FileUtils.mkdir_p(File.join(project_path, "app", "models"))
end

def create_config_directory
FileUtils.mkdir_p(File.join(project_path, 'config'))
FileUtils.mkdir_p(File.join(project_path, "config"))
end

def create_database_config
File.open(File.join(project_path, 'config', 'database.rb'), 'w') do |file|
File.open(File.join(project_path, "config", "database.rb"), "w") do |file|
file.write(database_config_template)
end
end

def create_gitignore
File.open(File.join(project_path, '.gitignore'), 'w') do |file|
File.open(File.join(project_path, ".gitignore"), "w") do |file|
file.write(gitignore_template)
end
end

def create_gemfile
File.open(File.join(project_path, 'Gemfile'), 'w') do |file|
File.open(File.join(project_path, "Gemfile"), "w") do |file|
file.write(gemfile_template)
end
end
Expand All @@ -70,7 +72,7 @@ def database_config_template
password: 'password',
port: 5432
},
#{" "}
test: {
adapter: 'postgres',
host: 'localhost',
Expand All @@ -79,7 +81,7 @@ def database_config_template
password: 'password',
port: 5432
},
#{" "}
production: {
adapter: 'postgres',
host: ENV['DB_HOST'],
Expand Down Expand Up @@ -114,4 +116,4 @@ def gemfile_template
RUBY
end
end
end
end
5 changes: 3 additions & 2 deletions lib/rubypit/config/database.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "sequel"
require "yaml"
require "erb"
Expand All @@ -15,7 +17,7 @@ def connect!
self.connection = Sequel.connect(config)
setup_connection_pool
connection
end
end

def connected?
!connection.nil? && !connection.pool.disconnected?
Expand All @@ -28,7 +30,6 @@ def load_configuration
yaml = ERB.new(File.read(config_file)).result
config = YAML.safe_load(yaml)[environment]


config.transform_keys(&:to_sym)
end

Expand Down
88 changes: 45 additions & 43 deletions spec/rubypit/cli_spec.rb
Original file line number Diff line number Diff line change
@@ -1,70 +1,72 @@
# frozen_string_literal: true

# spec/spec_helper.rb
require 'fileutils'
require 'thor'
require "fileutils"
require "thor"

RSpec.describe Rubypit::CLI do
describe '#create_project' do
describe "#create_project" do
let(:cli) { described_class.new }
let(:generator_double) { instance_double(Rubypit::ProjectGenerator) }

it 'creates a new project using ProjectGenerator' do
it "creates a new project using ProjectGenerator" do
expect(Rubypit::ProjectGenerator).to receive(:new)
.with('test_project')
.with("test_project")
.and_return(generator_double)
expect(generator_double).to receive(:generate)

cli.create_project('test_project')
cli.create_project("test_project")
end
end
end

RSpec.describe Rubypit::ProjectGenerator do
let(:project_name) { 'test_project' }
RSpec.describe Rubypit::ProjectGenerator do # rubocop:disable Metrics/BlockLength
let(:project_name) { "test_project" }
let(:generator) { described_class.new(project_name) }
let(:project_path) { File.join(Dir.pwd, project_name) }

describe '#initialize' do
it 'sets the project name' do
describe "#initialize" do
it "sets the project name" do
expect(generator.name).to eq(project_name)
end
end

describe '#generate' do
describe "#generate" do # rubocop:disable Metrics/BlockLength
before { generator.generate }
after { FileUtils.rm_rf(project_path) }

it 'creates the project directory structure' do
it "creates the project directory structure" do
expect(File.directory?(project_path)).to be true
expect(File.directory?(File.join(project_path, 'app'))).to be true
expect(File.directory?(File.join(project_path, 'app', 'models'))).to be true
expect(File.directory?(File.join(project_path, 'config'))).to be true
expect(File.directory?(File.join(project_path, "app"))).to be true
expect(File.directory?(File.join(project_path, "app", "models"))).to be true
expect(File.directory?(File.join(project_path, "config"))).to be true
end

it 'creates the database config file' do
config_path = File.join(project_path, 'config', 'database.rb')
it "creates the database config file" do
config_path = File.join(project_path, "config", "database.rb")
expect(File.exist?(config_path)).to be true

content = File.read(config_path)
expect(content).to include('development:')
expect(content).to include('test:')
expect(content).to include('production:')
expect(content).to include("development:")
expect(content).to include("test:")
expect(content).to include("production:")
expect(content).to include("database: '#{project_name}_development'")
end

it 'creates the .gitignore file' do
gitignore_path = File.join(project_path, '.gitignore')
it "creates the .gitignore file" do
gitignore_path = File.join(project_path, ".gitignore")
expect(File.exist?(gitignore_path)).to be true

content = File.read(gitignore_path)
expect(content).to include('.env')
expect(content).to include('.DS_Store')
expect(content).to include('config/database.yml')
expect(content).to include(".env")
expect(content).to include(".DS_Store")
expect(content).to include("config/database.yml")
end

it 'creates the Gemfile' do
gemfile_path = File.join(project_path, 'Gemfile')
it "creates the Gemfile" do
gemfile_path = File.join(project_path, "Gemfile")
expect(File.exist?(gemfile_path)).to be true

content = File.read(gemfile_path)
expect(content).to include("source 'https://rubygems.org'")
expect(content).to include("gem 'rubypit'")
Expand All @@ -73,37 +75,37 @@
end
end

describe 'private methods' do
describe '#project_path' do
it 'returns the full project path' do
describe "private methods" do # rubocop:disable Metrics/BlockLength
describe "#project_path" do
it "returns the full project path" do
expect(generator.send(:project_path)).to eq(project_path)
end
end

describe '#database_config_template' do
it 'includes the project name in database names' do
describe "#database_config_template" do
it "includes the project name in database names" do
template = generator.send(:database_config_template)
expect(template).to include("database: '#{project_name}_development'")
expect(template).to include("database: '#{project_name}_test'")
end
end

describe '#gitignore_template' do
it 'includes common ignored files' do
describe "#gitignore_template" do
it "includes common ignored files" do
template = generator.send(:gitignore_template)
expect(template).to include('.env')
expect(template).to include('.DS_Store')
expect(template).to include('config/database.yml')
expect(template).to include(".env")
expect(template).to include(".DS_Store")
expect(template).to include("config/database.yml")
end
end

describe '#gemfile_template' do
it 'includes required gems' do
describe "#gemfile_template" do
it "includes required gems" do
template = generator.send(:gemfile_template)
expect(template).to include("gem 'rubypit'")
expect(template).to include("gem 'sequel'")
expect(template).to include("gem 'pg'")
end
end
end
end
end
8 changes: 2 additions & 6 deletions spec/rubypit/config/database_spec.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
# frozen_string_literal: true

# rubocop:disable Metrics/BlockLength Layout/LeadingCommentSpace Layout/EmptyLineAfterMagicComment
require "sequel"
require "yaml"
require "erb"

RSpec.configure do |config|
config.before(:each) do
Rubypit::Config::Database.connection = nil
end
end

RSpec.describe Rubypit::Config::Database do
let(:test_config) do
{
Expand Down Expand Up @@ -186,3 +181,4 @@
end
end
end
# rubocop:enable Metrics/BlockLength Layout/LeadingCommentSpace Layout/EmptyLineAfterMagicComment
4 changes: 4 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,8 @@
config.after(:each) do
FileUtils.rm_rf(Dir["#{Dir.pwd}/test_*"])
end

config.before(:each) do
Rubypit::Config::Database.connection = nil
end
end

0 comments on commit 19fe483

Please sign in to comment.