From 8817b7e134d80cd66cfa6de20dfdeb4ed2270d49 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Mon, 7 Mar 2011 11:17:11 +0000 Subject: [PATCH 01/15] cli work in progress --- bin/faker | 7 ++++ faker.gemspec | 2 ++ lib/faker.rb | 1 + lib/faker/cli.rb | 84 ++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 94 insertions(+) create mode 100755 bin/faker create mode 100644 lib/faker/cli.rb diff --git a/bin/faker b/bin/faker new file mode 100755 index 0000000000..db03d18f23 --- /dev/null +++ b/bin/faker @@ -0,0 +1,7 @@ +#!/usr/bin/env ruby + +lib_dir = File.join(File.dirname(__FILE__), '..', 'lib') +$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir) + +require 'faker' +Faker::CLI.start diff --git a/faker.gemspec b/faker.gemspec index 73f95647d7..28baee0672 100644 --- a/faker.gemspec +++ b/faker.gemspec @@ -14,9 +14,11 @@ Gem::Specification.new do |s| s.rubyforge_project = "faker" s.add_dependency('i18n', '~> 0.4') + s.add_dependency('thor') s.files = `git ls-files -- lib/*`.split("\n") + %w(History.txt License.txt README.md) s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } s.require_paths = ["lib"] + end diff --git a/lib/faker.rb b/lib/faker.rb index 1ee19a67f8..91f444efb1 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -47,5 +47,6 @@ def fetch(key) require 'faker/name' require 'faker/phone_number' require 'faker/version' +require 'faker/cli' require 'extensions/array' diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb new file mode 100644 index 0000000000..f378feeee5 --- /dev/null +++ b/lib/faker/cli.rb @@ -0,0 +1,84 @@ +require 'thor' +require 'faker.rb' + +module Faker + class CLI < Thor + + default_task :help + + desc "Help", "Help text for the command line tool" + def help + puts 'help text to do' + end + + desc "Address", "Returns random data for an address" + def address(option) + case option + when 'street_name' + puts Faker::Address.street_name + when 'street_address' + puts Faker::Address.street_address + when 'secondary_address' + puts Faker::Address.secondary_address + when 'zip_code' + puts Faker::Address.zip_code + when 'street_suffix' + puts Faker::Address.street_suffix + when 'city_suffix' + puts Faker::Address.city_suffix + when 'city_prefix' + puts Faker::Address.city_prefix + when 'state_abbr' + puts Faker::Address.state_abbr + when 'state' + puts Faker::Address.state + when 'country' + puts Faker::Address.country + else + puts 'no option supplied' + end + end + + desc "Company", "Returns random data for a company" + def company(option) + case option + when 'name' + puts Faker::Company.name + when 'suffix' + puts Faker::Company.suffix + when 'catch_phrase' + puts Faker::Company.catch_phrase + when 'bs' + puts Faker::Company.bs + else + puts 'no option supplied' + end + end + + desc "Internet", "Returns random internet data" + def internet(option) + case option + when 'email' + puts Faker::Internet.email + when 'free_email' + puts Faker::Internet.free_email + when 'user_name' + puts Faker::Internet.user_name + when 'domain_name' + puts Faker::Internet.domain_name + when 'domain_word' + puts Faker::Internet.domain_word + when 'domain_suffix' + puts Faker::Internet.domain_suffix + when 'ip_v4_address' + puts Faker::Internet.ip_v4_address + when 'ip_v6_address' + puts Faker::Internet.ip_v6_address + else + puts 'no option supplied' + end + end + + end +end +# From 821501fa6d909be2b5b259e3a0b9382a8737d853 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Mon, 7 Mar 2011 16:33:12 +0000 Subject: [PATCH 02/15] starting documentation.. --- lib/faker/cli.rb | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index f378feeee5..abc24d6207 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -4,15 +4,10 @@ module Faker class CLI < Thor - default_task :help - desc "Help", "Help text for the command line tool" - def help - puts 'help text to do' - end - - desc "Address", "Returns random data for an address" - def address(option) + desc "address", "Returns random data for an address" + def address(option='') + option = false case option when 'street_name' puts Faker::Address.street_name @@ -39,7 +34,8 @@ def address(option) end end - desc "Company", "Returns random data for a company" + desc "company", "Returns random data for a company" + method_option :name, :aliases => "-n", :desc => "returns a company name" def company(option) case option when 'name' @@ -55,7 +51,7 @@ def company(option) end end - desc "Internet", "Returns random internet data" + desc "internet", "Returns random internet data" def internet(option) case option when 'email' From e41ff4ad48ef7e601fc9465d07984a5fd0555e85 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Mon, 7 Mar 2011 22:53:18 +0000 Subject: [PATCH 03/15] adding internet task --- lib/faker/cli.rb | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index abc24d6207..8491356b01 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -4,11 +4,11 @@ module Faker class CLI < Thor - - desc "address", "Returns random data for an address" - def address(option='') - option = false - case option + desc "address [-f street_name|street_address|secondary_address|zip_code|street_suffix|city_suffix|state_abbr|state|country]", + "Fake address data" + method_option :faker_type, :aliases => "-f", :desc => "the faker data type" + def address + case options.faker_type when 'street_name' puts Faker::Address.street_name when 'street_address' @@ -21,8 +21,6 @@ def address(option='') puts Faker::Address.street_suffix when 'city_suffix' puts Faker::Address.city_suffix - when 'city_prefix' - puts Faker::Address.city_prefix when 'state_abbr' puts Faker::Address.state_abbr when 'state' @@ -30,14 +28,16 @@ def address(option='') when 'country' puts Faker::Address.country else - puts 'no option supplied' + puts "Usage: `faker address -f=street_address`" + puts "Run `faker address --help` for a list of valid types" end end - desc "company", "Returns random data for a company" - method_option :name, :aliases => "-n", :desc => "returns a company name" - def company(option) - case option + desc "company [-f name|suffix|catch_phrase|bs]", + "Fake company data" + method_option :faker_type, :aliases => "-f", :desc => "the faker data type" + def company + case options.faker_type when 'name' puts Faker::Company.name when 'suffix' @@ -47,13 +47,16 @@ def company(option) when 'bs' puts Faker::Company.bs else - puts 'no option supplied' + puts "Usage: `faker company -f=name`" + puts "Run `faker company --help` for a list of valid types" end end - desc "internet", "Returns random internet data" - def internet(option) - case option + desc "internet [-f email|free_email|user_name|domain_name|ip_v4_address|ip_v6_address]", + "Fake internet data" + method_option :faker_type, :aliases => "-f", :desc => "the faker data type" + def internet + case options.faker_type when 'email' puts Faker::Internet.email when 'free_email' @@ -71,7 +74,8 @@ def internet(option) when 'ip_v6_address' puts Faker::Internet.ip_v6_address else - puts 'no option supplied' + puts "Usage: `faker internet -f=email`" + puts "Run `faker internet --help` for a list of valid types" end end From 87773eec9ea84f1184fc1fb4c8940f193c7dfcce Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 11 Mar 2011 13:01:24 +0000 Subject: [PATCH 04/15] adding all methods to cli --- lib/faker/cli.rb | 60 +++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 54 insertions(+), 6 deletions(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index 8491356b01..bf8da70dff 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -5,7 +5,7 @@ module Faker class CLI < Thor desc "address [-f street_name|street_address|secondary_address|zip_code|street_suffix|city_suffix|state_abbr|state|country]", - "Fake address data" + "Returns fake address data" method_option :faker_type, :aliases => "-f", :desc => "the faker data type" def address case options.faker_type @@ -29,12 +29,12 @@ def address puts Faker::Address.country else puts "Usage: `faker address -f=street_address`" - puts "Run `faker address --help` for a list of valid types" + puts "Run `faker help address` for a list of valid types" end end desc "company [-f name|suffix|catch_phrase|bs]", - "Fake company data" + "Returns fake company data" method_option :faker_type, :aliases => "-f", :desc => "the faker data type" def company case options.faker_type @@ -48,12 +48,12 @@ def company puts Faker::Company.bs else puts "Usage: `faker company -f=name`" - puts "Run `faker company --help` for a list of valid types" + puts "Run `faker help company` for a list of valid types" end end desc "internet [-f email|free_email|user_name|domain_name|ip_v4_address|ip_v6_address]", - "Fake internet data" + "Returns fake internet data" method_option :faker_type, :aliases => "-f", :desc => "the faker data type" def internet case options.faker_type @@ -75,10 +75,58 @@ def internet puts Faker::Internet.ip_v6_address else puts "Usage: `faker internet -f=email`" - puts "Run `faker internet --help` for a list of valid types" + puts "Run `faker help internet` for a list of valid types" end end + desc "lorum [-f words|sentence|sentences|paragraph|paragraphs -c 5]", + "Returns fake lorum data" + method_option :faker_type, :aliases => "-f", :desc => "the faker data type" + method_option :number, :type => :numeric, :aliases => "-n", :desc => "the number to return" + def lorum + case options.faker_type + when 'words' + puts options.number ? Faker::Lorem.words(options.number) : Faker::Lorem.words + when 'sentence' + puts options.number ? Faker::Lorem.sentence(options.number) : Faker::Lorem.sentence + when 'sentences' + puts options.number ? Faker::Lorem.sentences(options.number) : Faker::Lorem.sentences + when 'paragraph' + puts options.number ? Faker::Lorem.paragraph(options.number) : Faker::Lorem.paragraph + when 'paragraphs' + puts options.number ? Faker::Lorem.paragraphs(options.number) : Faker::Lorem.paragraphs + else + puts "Usage: `faker lorum -f words -n 5`" + puts "Run `faker help lorum` for a list of valid types" + end + end + + desc "name [-f name|first_name|last_name|prefix|suffix]", + "Returns fake name data" + method_option :faker_type, :aliases => "-f", :desc => "the faker data type" + def name + case options.faker_type + when 'name' + puts Faker::Name.name + when 'first_name' + puts Faker::Name.first_name + when 'last_name' + puts Faker::Name.last_name + when 'prefix' + puts Faker::Name.prefix + when 'suffix' + puts Faker::Name.suffix + else + puts "Usage: `faker name -f name`" + puts "Run `faker help name` for a list of valid types" + end + end + + desc "phone_number", + "Returns fake phone number data" + def phone_number + puts Faker::PhoneNumber.phone_number + end end end # From 70323bf98f0c3f0e3ddf6c2791556b39af32eff8 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 11 Mar 2011 13:09:23 +0000 Subject: [PATCH 05/15] adding cli interface to README --- README.md | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/README.md b/README.md index 04bbf22ca8..8d96efc86b 100644 --- a/README.md +++ b/README.md @@ -25,7 +25,19 @@ locale). If you'd prefer to set default_locale rather than locale, then you'll also need to add config.i18n.fallbacks.defaults = [:en] to your configuration to make the fallbacks work for Faker. +Command Line Usage +---------------- +Faker is also usable from the command line. All methods are available + + faker address -f street_name + faker company -f name + faker internet -f email + faker lorum -f sentences -n 10 + faker name -f first_name + faker phone_number +Run `faker help` for general help or there is also help for each data type. For the address type for example run `faker help address`. + Customization ------------ Since you may want to make addresses and other types of data look different From 74678c2d48233f39c39bf3edfb1124f6542e4b45 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 11 Mar 2011 13:10:46 +0000 Subject: [PATCH 06/15] adding bundler clarification for cli interface --- README.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/README.md b/README.md index 8d96efc86b..0720791c43 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,8 @@ Faker is also usable from the command line. All methods are available faker phone_number Run `faker help` for general help or there is also help for each data type. For the address type for example run `faker help address`. + +Note that if you installed using bundler you may need to use `bundle exec faker` rather than just `faker`. Customization ------------ From 9d2c95decc5b84bcbe9df30671b8a9d165bb8fdc Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 11 Mar 2011 14:21:22 +0000 Subject: [PATCH 07/15] adding cucumber tests to match test unit coverage --- .config/cucumber.yml | 1 + faker.gemspec | 4 +++- features/address.feature | 13 +++++++++++ features/internet.feature | 45 +++++++++++++++++++++++++++++++++++++++ features/support/env.rb | 2 ++ 5 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 .config/cucumber.yml create mode 100644 features/address.feature create mode 100644 features/internet.feature create mode 100644 features/support/env.rb diff --git a/.config/cucumber.yml b/.config/cucumber.yml new file mode 100644 index 0000000000..ecb685773e --- /dev/null +++ b/.config/cucumber.yml @@ -0,0 +1 @@ +default: --format progress diff --git a/faker.gemspec b/faker.gemspec index 28baee0672..e93fec8894 100644 --- a/faker.gemspec +++ b/faker.gemspec @@ -14,7 +14,9 @@ Gem::Specification.new do |s| s.rubyforge_project = "faker" s.add_dependency('i18n', '~> 0.4') - s.add_dependency('thor') + s.add_dependency 'thor' + s.add_development_dependency 'cucumber' + s.add_development_dependency 'aruba' s.files = `git ls-files -- lib/*`.split("\n") + %w(History.txt License.txt README.md) s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") diff --git a/features/address.feature b/features/address.feature new file mode 100644 index 0000000000..db79662eb9 --- /dev/null +++ b/features/address.feature @@ -0,0 +1,13 @@ +Feature: Address + In order to generate fake data + As a CLI + I want to be able to get fake address data + + Scenario: No options + When I run "faker address" + Then the output should contain "Run `faker help address` for a list of valid types" + + Scenario: Help + When I run "faker help address" + Then the output should contain "Usage:" + diff --git a/features/internet.feature b/features/internet.feature new file mode 100644 index 0000000000..cf4b7953b5 --- /dev/null +++ b/features/internet.feature @@ -0,0 +1,45 @@ +Feature: Internet + In order to generate fake data + As a CLI + I want to be able to get fake internet data + + Scenario: No options + When I run "faker internet" + Then the output should contain "Run `faker help internet` for a list of valid types" + + Scenario: Help + When I run "faker help internet" + Then the output should contain "Usage:" + + Scenario: Email + When I run "faker internet -f email" + Then the output should match /.+@.+\.\w+/ + + Scenario: Free Email + When I run "faker internet -f free_email" + Then the output should match /.+@(gmail|hotmail|yahoo)\.com/ + + Scenario: Username + When I run "faker internet -f user_name" + Then the output should match /[a-z]+((_|\.)[a-z]+)?/ + + Scenario: Domain Name + When I run "faker internet -f domain_name" + Then the output should match /\w+\.\w+/ + + Scenario: Domain Word + When I run "faker internet -f domain_word" + Then the output should match /^\w+$/ + + Scenario: Domain Suffix + When I run "faker internet -f domain_suffix" + Then the output should match /^\w+(\.\w+)?/ + + Scenario: IP4 Address + When I run "faker internet -f ip_v4_address" + Then the output should match /^\d+\.\d+\.\d+\.\d+$/ + + Scenario: IP6 Address + When I run "faker internet -f ip_v6_address" + Then the output should match /^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]{1,4}:){0,5}::([\da-fA-F]{1,4}:){0,5}[\da-fA-F]{1,4})$/ + diff --git a/features/support/env.rb b/features/support/env.rb new file mode 100644 index 0000000000..22c6de3a49 --- /dev/null +++ b/features/support/env.rb @@ -0,0 +1,2 @@ +require 'aruba/cucumber' +require 'faker' From c5242697c2c03081347a6084614028d365d2607c Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 16 Sep 2011 08:40:03 +0100 Subject: [PATCH 08/15] fix aruba deprecation warnings --- Gemfile.lock | 53 +++++++++++++++++++++++++++++++++++++++ features/address.feature | 4 +-- features/internet.feature | 20 +++++++-------- 3 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000000..dbb3f10089 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,53 @@ +PATH + remote: . + specs: + faker (1.0.0) + i18n (~> 0.4) + thor + +GEM + remote: http://rubygems.org/ + specs: + aruba (0.4.6) + bcat (>= 0.6.1) + childprocess (>= 0.2.0) + cucumber (>= 1.0.2) + rdiscount (>= 1.6.8) + rspec (>= 2.6.0) + bcat (0.6.2) + rack (~> 1.0) + builder (3.0.0) + childprocess (0.2.2) + ffi (~> 1.0.6) + cucumber (1.0.6) + builder (>= 2.1.2) + diff-lcs (>= 1.1.2) + gherkin (~> 2.4.18) + json (>= 1.4.6) + term-ansicolor (>= 1.0.6) + diff-lcs (1.1.3) + ffi (1.0.9) + gherkin (2.4.18) + json (>= 1.4.6) + i18n (0.6.0) + json (1.6.0) + rack (1.3.2) + rdiscount (1.6.8) + rspec (2.6.0) + rspec-core (~> 2.6.0) + rspec-expectations (~> 2.6.0) + rspec-mocks (~> 2.6.0) + rspec-core (2.6.4) + rspec-expectations (2.6.0) + diff-lcs (~> 1.1.2) + rspec-mocks (2.6.0) + term-ansicolor (1.0.6) + thor (0.14.6) + +PLATFORMS + ruby + +DEPENDENCIES + aruba + cucumber + faker! diff --git a/features/address.feature b/features/address.feature index db79662eb9..bdf11a0c58 100644 --- a/features/address.feature +++ b/features/address.feature @@ -4,10 +4,10 @@ Feature: Address I want to be able to get fake address data Scenario: No options - When I run "faker address" + When I run `faker address` Then the output should contain "Run `faker help address` for a list of valid types" Scenario: Help - When I run "faker help address" + When I run `faker help address` Then the output should contain "Usage:" diff --git a/features/internet.feature b/features/internet.feature index cf4b7953b5..929230e5ab 100644 --- a/features/internet.feature +++ b/features/internet.feature @@ -4,42 +4,42 @@ Feature: Internet I want to be able to get fake internet data Scenario: No options - When I run "faker internet" + When I run `faker internet` Then the output should contain "Run `faker help internet` for a list of valid types" Scenario: Help - When I run "faker help internet" + When I run `faker help internet` Then the output should contain "Usage:" Scenario: Email - When I run "faker internet -f email" + When I run `faker internet -f email` Then the output should match /.+@.+\.\w+/ Scenario: Free Email - When I run "faker internet -f free_email" + When I run `faker internet -f free_email` Then the output should match /.+@(gmail|hotmail|yahoo)\.com/ Scenario: Username - When I run "faker internet -f user_name" + When I run `faker internet -f user_name` Then the output should match /[a-z]+((_|\.)[a-z]+)?/ Scenario: Domain Name - When I run "faker internet -f domain_name" + When I run `faker internet -f domain_name` Then the output should match /\w+\.\w+/ Scenario: Domain Word - When I run "faker internet -f domain_word" + When I run `faker internet -f domain_word` Then the output should match /^\w+$/ Scenario: Domain Suffix - When I run "faker internet -f domain_suffix" + When I run `faker internet -f domain_suffix` Then the output should match /^\w+(\.\w+)?/ Scenario: IP4 Address - When I run "faker internet -f ip_v4_address" + When I run `faker internet -f ip_v4_address` Then the output should match /^\d+\.\d+\.\d+\.\d+$/ Scenario: IP6 Address - When I run "faker internet -f ip_v6_address" + When I run `faker internet -f ip_v6_address` Then the output should match /^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]{1,4}:){0,5}::([\da-fA-F]{1,4}:){0,5}[\da-fA-F]{1,4})$/ From cb08338e55fdb8763bb73c1bf3988fbfd4c6b1a1 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 16 Sep 2011 08:42:18 +0100 Subject: [PATCH 09/15] fix case warnings --- lib/faker/cli.rb | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index bf8da70dff..e6d3a5d683 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -49,7 +49,7 @@ def company else puts "Usage: `faker company -f=name`" puts "Run `faker help company` for a list of valid types" - end + end end desc "internet [-f email|free_email|user_name|domain_name|ip_v4_address|ip_v6_address]", @@ -76,7 +76,7 @@ def internet else puts "Usage: `faker internet -f=email`" puts "Run `faker help internet` for a list of valid types" - end + end end desc "lorum [-f words|sentence|sentences|paragraph|paragraphs -c 5]", @@ -98,7 +98,7 @@ def lorum else puts "Usage: `faker lorum -f words -n 5`" puts "Run `faker help lorum` for a list of valid types" - end + end end desc "name [-f name|first_name|last_name|prefix|suffix]", @@ -119,7 +119,7 @@ def name else puts "Usage: `faker name -f name`" puts "Run `faker help name` for a list of valid types" - end + end end desc "phone_number", From 1942740b77d15af3cf5805437c2c8b2cbf73b4a4 Mon Sep 17 00:00:00 2001 From: George Ornbo Date: Fri, 16 Sep 2011 08:49:54 +0100 Subject: [PATCH 10/15] fix circular include --- lib/faker/cli.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index e6d3a5d683..0434e4ec23 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -1,5 +1,4 @@ require 'thor' -require 'faker.rb' module Faker class CLI < Thor From c44de240a340cdfe51072bfefbcfde1f5f36eb4a Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 17 Sep 2018 19:14:56 -0300 Subject: [PATCH 11/15] Remove cucumber --- .config/cucumber.yml | 1 - features/address.feature | 13 ----------- features/internet.feature | 45 --------------------------------------- features/support/env.rb | 4 ---- lib/faker.rb | 1 - 5 files changed, 64 deletions(-) delete mode 100644 .config/cucumber.yml delete mode 100644 features/address.feature delete mode 100644 features/internet.feature delete mode 100644 features/support/env.rb diff --git a/.config/cucumber.yml b/.config/cucumber.yml deleted file mode 100644 index ecb685773e..0000000000 --- a/.config/cucumber.yml +++ /dev/null @@ -1 +0,0 @@ -default: --format progress diff --git a/features/address.feature b/features/address.feature deleted file mode 100644 index bdf11a0c58..0000000000 --- a/features/address.feature +++ /dev/null @@ -1,13 +0,0 @@ -Feature: Address - In order to generate fake data - As a CLI - I want to be able to get fake address data - - Scenario: No options - When I run `faker address` - Then the output should contain "Run `faker help address` for a list of valid types" - - Scenario: Help - When I run `faker help address` - Then the output should contain "Usage:" - diff --git a/features/internet.feature b/features/internet.feature deleted file mode 100644 index 929230e5ab..0000000000 --- a/features/internet.feature +++ /dev/null @@ -1,45 +0,0 @@ -Feature: Internet - In order to generate fake data - As a CLI - I want to be able to get fake internet data - - Scenario: No options - When I run `faker internet` - Then the output should contain "Run `faker help internet` for a list of valid types" - - Scenario: Help - When I run `faker help internet` - Then the output should contain "Usage:" - - Scenario: Email - When I run `faker internet -f email` - Then the output should match /.+@.+\.\w+/ - - Scenario: Free Email - When I run `faker internet -f free_email` - Then the output should match /.+@(gmail|hotmail|yahoo)\.com/ - - Scenario: Username - When I run `faker internet -f user_name` - Then the output should match /[a-z]+((_|\.)[a-z]+)?/ - - Scenario: Domain Name - When I run `faker internet -f domain_name` - Then the output should match /\w+\.\w+/ - - Scenario: Domain Word - When I run `faker internet -f domain_word` - Then the output should match /^\w+$/ - - Scenario: Domain Suffix - When I run `faker internet -f domain_suffix` - Then the output should match /^\w+(\.\w+)?/ - - Scenario: IP4 Address - When I run `faker internet -f ip_v4_address` - Then the output should match /^\d+\.\d+\.\d+\.\d+$/ - - Scenario: IP6 Address - When I run `faker internet -f ip_v6_address` - Then the output should match /^(((([1]?\d)?\d|2[0-4]\d|25[0-5])\.){3}(([1]?\d)?\d|2[0-4]\d|25[0-5]))|([\da-fA-F]{1,4}(\:[\da-fA-F]{1,4}){7})|(([\da-fA-F]{1,4}:){0,5}::([\da-fA-F]{1,4}:){0,5}[\da-fA-F]{1,4})$/ - diff --git a/features/support/env.rb b/features/support/env.rb deleted file mode 100644 index af5570284d..0000000000 --- a/features/support/env.rb +++ /dev/null @@ -1,4 +0,0 @@ -# frozen_string_literal: true - -require 'aruba/cucumber' -require 'faker' diff --git a/lib/faker.rb b/lib/faker.rb index c64fb02c0a..4ea5fcc3bf 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -229,5 +229,4 @@ def rand(max = nil) %w[faker helpers].each do |path| Dir.glob(File.join(File.dirname(__FILE__), path, '*.rb')).sort.each { |file| require file } - Dir.glob(File.join(File.dirname(__FILE__), path, '*.rb')).sort.each { |file| require file } end From e03164ba0b889cd903cc83655d7620a76ee8a194 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 17 Sep 2018 21:21:02 -0300 Subject: [PATCH 12/15] Add CLI and dynamic logic --- Gemfile.lock | 15 ++--- exe/faker | 6 ++ faker.gemspec | 25 +++++--- lib/faker.rb | 7 +++ lib/faker/cli.rb | 131 ++++----------------------------------- test/test_determinism.rb | 6 +- 6 files changed, 51 insertions(+), 139 deletions(-) create mode 100755 exe/faker diff --git a/Gemfile.lock b/Gemfile.lock index 32a4dbdc20..2d23233062 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -3,7 +3,7 @@ PATH specs: faker (1.9.1) i18n (>= 0.7) - thor + thor (= 0.20.0) GEM remote: https://rubygems.org/ @@ -47,13 +47,14 @@ PLATFORMS ruby DEPENDENCIES + bundler (= 1.16.4) faker! - minitest - rake - rubocop - simplecov - test-unit - timecop + minitest (= 5.11.3) + rake (= 12.3.1) + rubocop (= 0.59.1) + simplecov (= 0.16.1) + test-unit (= 3.2.8) + timecop (= 0.9.1) BUNDLED WITH 1.16.4 diff --git a/exe/faker b/exe/faker new file mode 100755 index 0000000000..b3550f3ffc --- /dev/null +++ b/exe/faker @@ -0,0 +1,6 @@ +#!/usr/bin/env ruby +# frozen_string_literal: true + +require 'faker' + +Faker::CLI.start(ARGV) diff --git a/faker.gemspec b/faker.gemspec index 741058c017..af15720393 100644 --- a/faker.gemspec +++ b/faker.gemspec @@ -9,22 +9,29 @@ Gem::Specification.new do |spec| spec.platform = Gem::Platform::RUBY spec.authors = ['Benjamin Curtis'] spec.email = ['benjamin.curtis@gmail.com'] - spec.homepage = 'https://github.com/stympy/faker' + spec.summary = 'Easily generate fake data' spec.description = 'Faker, a port of Data::Faker from Perl, is used to easily generate fake data: names, addresses, phone numbers, etc.' + spec.homepage = 'https://github.com/stympy/faker' spec.license = 'MIT' spec.files = Dir['lib/**/*'] + %w[History.md License.txt CHANGELOG.md README.md] + spec.bindir = 'exe' + spec.executables = spec.files.grep(%r{^exe/}) do |file| + File.basename(file) + end spec.require_paths = ['lib'] spec.required_ruby_version = '>= 2.3' - spec.add_runtime_dependency('i18n', '>= 0.7') - spec.add_runtime_dependency 'thor' + spec.add_dependency('thor', '0.20.0') + + spec.add_development_dependency('bundler', '1.16.4') + spec.add_development_dependency('minitest', '5.11.3') + spec.add_development_dependency('rake', '12.3.1') + spec.add_development_dependency('rubocop', '0.59.1') + spec.add_development_dependency('simplecov', '0.16.1') + spec.add_development_dependency('test-unit', '3.2.8') + spec.add_development_dependency('timecop', '0.9.1') - spec.add_development_dependency('minitest') - spec.add_development_dependency('rake') - spec.add_development_dependency('rubocop') - spec.add_development_dependency('simplecov') - spec.add_development_dependency('test-unit') - spec.add_development_dependency('timecop') + spec.add_runtime_dependency('i18n', '>= 0.7') end diff --git a/lib/faker.rb b/lib/faker.rb index 4ea5fcc3bf..d652d6f4e7 100644 --- a/lib/faker.rb +++ b/lib/faker.rb @@ -223,6 +223,13 @@ def rand(max = nil) 0 end end + + # TODO: Remove this evil eval method + # rubocop:disable Style/EvalWithLocation, Security/Eval + def module_methods(subclass) + eval("Faker::#{subclass}.public_methods(false)") - Faker::Base.public_methods(false) + end + # rubocop:enable Style/EvalWithLocation, Security/Eval end end end diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index a82d733fc4..9f58855ff6 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -4,128 +4,19 @@ module Faker class CLI < Thor - desc 'address [-f street_name|street_address|secondary_address|zip_code|street_suffix|city_suffix|state_abbr|state|country]', - 'Returns fake address data' - method_option :faker_type, aliases: '-f', desc: 'the faker data type' - def address - case options.faker_type - when 'street_name' - puts Faker::Address.street_name - when 'street_address' - puts Faker::Address.street_address - when 'secondary_address' - puts Faker::Address.secondary_address - when 'zip_code' - puts Faker::Address.zip_code - when 'street_suffix' - puts Faker::Address.street_suffix - when 'city_suffix' - puts Faker::Address.city_suffix - when 'state_abbr' - puts Faker::Address.state_abbr - when 'state' - puts Faker::Address.state - when 'country' - puts Faker::Address.country - else - puts 'Usage: `faker address -f=street_address`' - puts 'Run `faker help address` for a list of valid types' - end - end + Faker.constants.sort.each do |constant| + desc "#{constant.downcase} [-f #{Faker::Base.module_methods(constant)}]", + "Returns fake #{constant.downcase} data" + option :faker_type, aliases: '-f', desc: 'the faker data type' - desc 'company [-f name|suffix|catch_phrase|bs]', - 'Returns fake company data' - method_option :faker_type, aliases: '-f', desc: 'the faker data type' - def company - case options.faker_type - when 'name' - puts Faker::Company.name - when 'suffix' - puts Faker::Company.suffix - when 'catch_phrase' - puts Faker::Company.catch_phrase - when 'bs' - puts Faker::Company.bs - else - puts 'Usage: `faker company -f=name`' - puts 'Run `faker help company` for a list of valid types' + define_method :"#{constant.downcase}" do + begin + puts Object.const_get("Faker::#{constant.capitalize}.#{options.faker_type}") + rescue I18n::MissingTranslationData + puts 'Usage: `faker address -f=street_address`' + puts 'Run `faker help address` for a list of valid types' + end end end - - desc 'internet [-f email|free_email|user_name|domain_name|ip_v4_address|ip_v6_address]', - 'Returns fake internet data' - method_option :faker_type, aliases: '-f', desc: 'the faker data type' - def internet - case options.faker_type - when 'email' - puts Faker::Internet.email - when 'free_email' - puts Faker::Internet.free_email - when 'user_name' - puts Faker::Internet.user_name - when 'domain_name' - puts Faker::Internet.domain_name - when 'domain_word' - puts Faker::Internet.domain_word - when 'domain_suffix' - puts Faker::Internet.domain_suffix - when 'ip_v4_address' - puts Faker::Internet.ip_v4_address - when 'ip_v6_address' - puts Faker::Internet.ip_v6_address - else - puts 'Usage: `faker internet -f=email`' - puts 'Run `faker help internet` for a list of valid types' - end - end - - desc 'lorum [-f words|sentence|sentences|paragraph|paragraphs -c 5]', - 'Returns fake lorum data' - method_option :faker_type, aliases: '-f', desc: 'the faker data type' - method_option :number, type: :numeric, aliases: '-n', desc: 'the number to return' - def lorum - case options.faker_type - when 'words' - puts options.number ? Faker::Lorem.words(options.number) : Faker::Lorem.words - when 'sentence' - puts options.number ? Faker::Lorem.sentence(options.number) : Faker::Lorem.sentence - when 'sentences' - puts options.number ? Faker::Lorem.sentences(options.number) : Faker::Lorem.sentences - when 'paragraph' - puts options.number ? Faker::Lorem.paragraph(options.number) : Faker::Lorem.paragraph - when 'paragraphs' - puts options.number ? Faker::Lorem.paragraphs(options.number) : Faker::Lorem.paragraphs - else - puts 'Usage: `faker lorum -f words -n 5`' - puts 'Run `faker help lorum` for a list of valid types' - end - end - - desc 'name [-f name|first_name|last_name|prefix|suffix]', - 'Returns fake name data' - method_option :faker_type, aliases: '-f', desc: 'the faker data type' - def name - case options.faker_type - when 'name' - puts Faker::Name.name - when 'first_name' - puts Faker::Name.first_name - when 'last_name' - puts Faker::Name.last_name - when 'prefix' - puts Faker::Name.prefix - when 'suffix' - puts Faker::Name.suffix - else - puts 'Usage: `faker name -f name`' - puts 'Run `faker help name` for a list of valid types' - end - end - - desc 'phone_number', - 'Returns fake phone number data' - def phone_number - puts Faker::PhoneNumber.phone_number - end end end diff --git a/test/test_determinism.rb b/test/test_determinism.rb index 4509fd4dfb..1d1d3990c5 100644 --- a/test/test_determinism.rb +++ b/test/test_determinism.rb @@ -1,7 +1,7 @@ # frozen_string_literal: true require_relative 'test_helper' -# rubocop:disable Security/Eval,Style/EvalWithLocation +# rubocop:disable Security/Eval class TestDeterminism < Test::Unit::TestCase def setup @all_methods = all_methods.freeze @@ -50,9 +50,9 @@ def subclasses end def subclass_methods(subclass) - eval("Faker::#{subclass}.public_methods(false) - Faker::Base.public_methods(false)").sort.map do |method| + Faker::Base.module_methods(subclass).sort.map do |method| "Faker::#{subclass}.#{method}" end.sort end end -# rubocop:enable Security/Eval,Style/EvalWithLocation +# rubocop:enable Security/Eval From 16cb321a842df20369780d2f7b5d266f9740e713 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 17 Sep 2018 21:25:25 -0300 Subject: [PATCH 13/15] Remove unnecessary bin file --- bin/faker | 8 -------- 1 file changed, 8 deletions(-) delete mode 100755 bin/faker diff --git a/bin/faker b/bin/faker deleted file mode 100755 index 8912960c05..0000000000 --- a/bin/faker +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env ruby -# frozen_string_literal: true - -lib_dir = File.join(File.dirname(__FILE__), '..', 'lib') -$LOAD_PATH.unshift lib_dir if File.directory?(lib_dir) - -require 'faker' -Faker::CLI.start From 3b056995856db6957c3553731a3d45d064d61816 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 17 Sep 2018 21:27:32 -0300 Subject: [PATCH 14/15] Add join method to fix CLI descs --- lib/faker/cli.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index 9f58855ff6..90b0f78d09 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -5,7 +5,7 @@ module Faker class CLI < Thor Faker.constants.sort.each do |constant| - desc "#{constant.downcase} [-f #{Faker::Base.module_methods(constant)}]", + desc "#{constant.downcase} [-f #{Faker::Base.module_methods(constant).join('|')}]", "Returns fake #{constant.downcase} data" option :faker_type, aliases: '-f', desc: 'the faker data type' From 2ec4901b6b9924abcf0b4246163fea1406a5c686 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Mon, 17 Sep 2018 21:30:30 -0300 Subject: [PATCH 15/15] Fix CLI error message --- lib/faker/cli.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/faker/cli.rb b/lib/faker/cli.rb index 90b0f78d09..386c12eb50 100644 --- a/lib/faker/cli.rb +++ b/lib/faker/cli.rb @@ -13,8 +13,8 @@ class CLI < Thor begin puts Object.const_get("Faker::#{constant.capitalize}.#{options.faker_type}") rescue I18n::MissingTranslationData - puts 'Usage: `faker address -f=street_address`' - puts 'Run `faker help address` for a list of valid types' + puts "Usage: `faker #{constant.downcase} -f=#{options.faker_type}`" + puts "Run `faker help #{constant.downcase}` for a list of valid types" end end end