diff --git a/.rubocop.yml b/.rubocop.yml new file mode 100644 index 0000000..6995009 --- /dev/null +++ b/.rubocop.yml @@ -0,0 +1,4 @@ +inherit_from: .rubocop_todo.yml + +inherit_gem: + rubocop-lts: rubocop-lts1_8.yml diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml new file mode 100644 index 0000000..854fc4f --- /dev/null +++ b/.rubocop_todo.yml @@ -0,0 +1,54 @@ +# This configuration was generated by +# `rubocop --auto-gen-config` +# on 2022-12-11 23:40:03 +0700 using RuboCop version 0.41.2. +# The point is for the user to remove these configuration records +# one by one as the offenses are removed from the code base. +# Note that changes in the inspected code, or installation of new +# versions of RuboCop, may require this file to be generated again. + +# Offense count: 1 +Lint/ParenthesesAsGroupedExpression: + Exclude: + - 'spec/os_spec.rb' + +# Offense count: 2 +Metrics/AbcSize: + Max: 31 + +# Offense count: 1 +# Configuration parameters: CountComments. +Metrics/ClassLength: + Max: 234 + +# Offense count: 2 +Metrics/CyclomaticComplexity: + Max: 9 + +# Offense count: 22 +# Configuration parameters: AllowHeredoc, AllowURI, URISchemes. +# URISchemes: http, https +Metrics/LineLength: + Max: 144 + +# Offense count: 6 +# Configuration parameters: CountComments. +Metrics/MethodLength: + Max: 26 + +# Offense count: 3 +Metrics/PerceivedComplexity: + Max: 10 + +# Offense count: 1 +# Cop supports --auto-correct. +# Configuration parameters: EnforcedStyle, SupportedStyles, UseHashRocketsWithSymbolValues, PreferHashRocketsForNonAlnumEndingSymbols. +# SupportedStyles: ruby19, ruby19_no_mixed_keys, hash_rockets +Style/HashSyntax: + Exclude: + - 'Rakefile' + +# Offense count: 2 +Style/IfInsideElse: + Exclude: + - 'lib/os.rb' + - 'spec/os_spec.rb' diff --git a/Gemfile b/Gemfile index c80ee36..fceb122 100644 --- a/Gemfile +++ b/Gemfile @@ -1,3 +1,4 @@ -source "http://rubygems.org" +# encoding: utf-8 +source 'http://rubygems.org' gemspec diff --git a/Gemfile.lock b/Gemfile.lock index 935aa3a..b83b180 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,6 +6,7 @@ PATH GEM remote: http://rubygems.org/ specs: + ast (2.4.2) coderay (1.1.3) diff-lcs (1.5.0) ffi (1.15.5) @@ -33,10 +34,15 @@ GEM notiffany (0.1.3) nenv (~> 0.1) shellany (~> 0.0) + parser (2.4.0.2) + ast (~> 2.3) power_assert (2.0.2) + powerpack (0.1.3) pry (0.14.1) coderay (~> 1.1) method_source (~> 1.0) + rainbow (2.2.2) + rake rake (10.5.0) rb-fsevent (0.11.2) rb-inotify (0.10.1) @@ -54,10 +60,23 @@ GEM diff-lcs (>= 1.2.0, < 2.0) rspec-support (~> 3.12.0) rspec-support (3.12.0) + rubocop (0.41.2) + parser (>= 2.3.1.1, < 3.0) + powerpack (~> 0.1) + rainbow (>= 1.99.1, < 3.0) + ruby-progressbar (~> 1.7) + unicode-display_width (~> 1.0, >= 1.0.1) + rubocop-lts (2.0.5) + rubocop-ruby1_9 (~> 1.0.5) + rubocop-ruby1_9 (1.0.5) + parser (= 2.4.0.2) + rubocop (= 0.41.2) + ruby-progressbar (1.11.0) shellany (0.0.1) test-unit (3.5.5) power_assert thor (1.2.1) + unicode-display_width (1.8.0) PLATFORMS x86_64-darwin-19 @@ -67,6 +86,7 @@ DEPENDENCIES os! rake (~> 10.5) rspec (~> 3.12) + rubocop-lts (~> 2.0) test-unit (~> 3.5) BUNDLED WITH diff --git a/Guardfile b/Guardfile index db914c1..9341543 100644 --- a/Guardfile +++ b/Guardfile @@ -1,7 +1,8 @@ +# encoding: utf-8 # More info at https://github.com/guard/guard#readme -guard :rspec, cmd: "bundle exec rspec" do - require "guard/rspec/dsl" +guard :rspec, :cmd => 'bundle exec rspec' do + require 'guard/rspec/dsl' dsl = Guard::RSpec::Dsl.new(self) # Feel free to open issues for suggestions and improvements diff --git a/Rakefile b/Rakefile index b537520..241ada3 100644 --- a/Rakefile +++ b/Rakefile @@ -1,6 +1,32 @@ -require 'rubygems' if RUBY_VERSION < '1.9.0' +# encoding: utf-8 +# frozen_string_literal: true -require 'rspec/core/rake_task' -RSpec::Core::RakeTask.new(:spec) -task :default => :spec +# !/usr/bin/env rake +require 'bundler/gem_tasks' + +begin + require 'rspec/core/rake_task' + RSpec::Core::RakeTask.new(:spec) +rescue LoadError + desc 'spec task stub' + task :spec do + warn 'rspec is disabled' + end +end +desc 'alias test task to spec' +task :test => :spec + +begin + require 'rubocop/rake_task' + RuboCop::RakeTask.new do |task| + task.options = ['-D'] # Display the name of the failing cops + end +rescue LoadError + desc 'rubocop task stub' + task :rubocop do + warn 'RuboCop is disabled' + end +end + +task default: %i[test rubocop] diff --git a/lib/os.rb b/lib/os.rb index c65764d..16406cc 100644 --- a/lib/os.rb +++ b/lib/os.rb @@ -1,317 +1,303 @@ -require 'rbconfig' -require 'yaml' - -# a set of friendly files for determining your Ruby runtime -# treats cygwin as linux -# also treats IronRuby on mono as...linux -class OS - attr_reader :config - - def self.config - @config ||= RbConfig::CONFIG - end - - # true if on windows [and/or jruby] - # false if on linux or cygwin on windows - - def self.windows? - @windows ||= begin - if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin - false - elsif ENV['OS'] == 'Windows_NT' - true - else - false - end - end - - end - - # true for linux, os x, cygwin - def self.posix? - @posix ||= - begin - if OS.windows? - begin - begin - # what if we're on interix... - # untested, of course - Process.wait fork{} - true - rescue NotImplementedError, NoMethodError - false - end - end - else - # assume non windows is posix - true - end - end - - end - - # true for linux, false for windows, os x, cygwin - def self.linux? - if (host_os =~ /linux/) - true - else - false - end - end - - def self.freebsd? - if (host_os =~ /freebsd/) - true - else - false - end - end - - def self.iron_ruby? - @iron_ruby ||= begin - if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby') - true - else - false - end - end - end - - def self.bits - @bits ||= begin - if host_cpu =~ /_64$/ || RUBY_PLATFORM =~ /x86_64/ - 64 - elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64":http://www.ruby-forum.com/topic/202173#880613 - ENV_JAVA['sun.arch.data.model'].to_i - elsif host_cpu == 'i386' - 32 - elsif host_os =~ /32$/ # mingw32, mswin32 - 32 - else # cygwin only...I think - if 1.size == 8 - 64 - else - 32 - end - end - end - end - - - def self.java? - @java ||= begin - if RUBY_PLATFORM =~ /java/ - true - else - false - end - end - end - - def self.ruby_bin - @ruby_exe ||= begin - File::join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] - end - end - - def self.mac? - @mac = begin - if host_os =~ /darwin/ - true - else - false - end - end - end - - def self.osx? - mac? - end - - def self.x? - mac? - end - - - # amount of memory the current process "is using", in RAM - # (doesn't include any swap memory that it may be using, just that in actual RAM) - # raises 'unknown' on jruby currently - def self.rss_bytes - # attempt to do this in a jruby friendly way - if OS::Underlying.windows? - # MRI, Java, IronRuby, Cygwin - if OS.java? - # no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P - require 'java' - mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean - mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used - else - wmi = nil - begin - require 'win32ole' - wmi = WIN32OLE.connect("winmgmts://") - rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh] - raise 'rss unknown for this platform ' + e.to_s - end - processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}") - memory_used = nil - # only allow for one process... - for process in processes - raise "multiple processes same pid?" if memory_used - memory_used = process.WorkingSetSize.to_i - end - memory_used - end - elsif OS.posix? # linux [though I've heard it works in OS X] - `ps -o rss= -p #{Process.pid}`.to_i * 1024 # in kiloBytes - else - raise 'unknown rss for this platform' - end - end - - class Underlying - - def self.bsd? - OS.osx? - end - - def self.windows? - ENV['OS'] == 'Windows_NT' - end - - def self.linux? - OS.host_os =~ /linux/ ? true : false - end - - def self.docker? - system('grep -q docker /proc/self/cgroup') if OS.linux? - end - - end - - def self.cygwin? - @cygwin = begin - if RUBY_PLATFORM =~ /-cygwin/ - true - else - false - end - end - end - - def self.dev_null # File::NULL in 1.9.3+ - @dev_null ||= begin - if OS.windows? - "NUL" - else - "/dev/null" - end - end - end - - # provides easy way to see the relevant config entries - def self.report - relevant_keys = [ - 'arch', - 'host', - 'host_cpu', - 'host_os', - 'host_vendor', - 'target', - 'target_cpu', - 'target_os', - 'target_vendor', - ] - RbConfig::CONFIG.reject {|key, val| !relevant_keys.include? key }.merge({'RUBY_PLATFORM' => RUBY_PLATFORM}).to_yaml - end - - def self.cpu_count - @cpu_count ||= - case RUBY_PLATFORM - when /darwin9/ - `hwprefs cpu_count`.to_i - when /darwin10/ - (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i - when /linux/ - `cat /proc/cpuinfo | grep processor | wc -l`.to_i - when /freebsd/ - `sysctl -n hw.ncpu`.to_i - else - if RbConfig::CONFIG['host_os'] =~ /darwin/ - (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i - elsif self.windows? - # ENV counts hyper threaded...not good. - # out = ENV['NUMBER_OF_PROCESSORS'].to_i - require 'win32ole' - wmi = WIN32OLE.connect("winmgmts://") - cpu = wmi.ExecQuery("select NumberOfCores from Win32_Processor") # don't count hyper-threaded in this - cpu.to_enum.first.NumberOfCores - else - raise 'unknown platform processor_count' - end - end - end - - def self.open_file_command - if OS.cygwin? - "cygstart" - elsif OS.doze? - "start" - elsif OS.mac? - "open" - else - "xdg-open" - end - end - - def self.app_config_path(name) - if OS.doze? - if ENV['LOCALAPPDATA'] - return File.join(ENV['LOCALAPPDATA'], name) - end - - File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data', name - elsif OS.mac? - File.join ENV['HOME'], 'Library', 'Application Support', name - else - if ENV['XDG_CONFIG_HOME'] - return File.join(ENV['XDG_CONFIG_HOME'], name) - end - - File.join ENV['HOME'], '.config', name - end - end - - def self.parse_os_release - if OS.linux? && File.exist?('/etc/os-release') - output = {} - - File.read('/etc/os-release').each_line do |line| - parsed_line = line.chomp.tr('"', '').split('=') - next if parsed_line.empty? - output[parsed_line[0].to_sym] = parsed_line[1] - end - output - else - raise "File /etc/os-release doesn't exists or not Linux" - end - end - - class << self - alias :doze? :windows? # a joke name but I use it and like it :P - alias :jruby? :java? - - # delegators for relevant config values - %w(host host_cpu host_os).each do |method_name| - define_method(method_name) { config[method_name] } - end - - end - - private - - def self.hwprefs_available? - `which hwprefs` != '' - end - -end +# encoding: utf-8 +require 'rbconfig' +require 'yaml' + +# a set of friendly files for determining your Ruby runtime +# treats cygwin as linux +# also treats IronRuby on mono as...linux +class OS + attr_reader :config + + def self.config + @config ||= RbConfig::CONFIG + end + + # true if on windows [and/or jruby] + # false if on linux or cygwin on windows + + def self.windows? + @windows ||= begin + if RUBY_PLATFORM =~ /cygwin/ # i386-cygwin + false + elsif ENV['OS'] == 'Windows_NT' + true + else + false + end + end + end + + # true for linux, os x, cygwin + def self.posix? + @posix ||= + begin + if OS.windows? + begin + begin + # what if we're on interix... + # untested, of course + Process.wait fork {} + true + rescue NotImplementedError, NoMethodError + false + end + end + else + # assume non windows is posix + true + end + end + end + + # true for linux, false for windows, os x, cygwin + def self.linux? + if host_os =~ /linux/ + true + else + false + end + end + + def self.freebsd? + if host_os =~ /freebsd/ + true + else + false + end + end + + def self.iron_ruby? + @iron_ruby ||= begin + if defined?(RUBY_ENGINE) && (RUBY_ENGINE == 'ironruby') + true + else + false + end + end + end + + def self.bits + @bits ||= begin + if host_cpu =~ /_64$/ || RUBY_PLATFORM =~ /x86_64/ + 64 + elsif RUBY_PLATFORM == 'java' && ENV_JAVA['sun.arch.data.model'] # "32" or "64":http://www.ruby-forum.com/topic/202173#880613 + ENV_JAVA['sun.arch.data.model'].to_i + elsif host_cpu == 'i386' + 32 + elsif host_os =~ /32$/ # mingw32, mswin32 + 32 + else # cygwin only...I think + if 1.size == 8 + 64 + else + 32 + end + end + end + end + + def self.java? + @java ||= begin + if RUBY_PLATFORM =~ /java/ + true + else + false + end + end + end + + def self.ruby_bin + @ruby_exe ||= begin + File.join(config['bindir'], config['ruby_install_name']) + config['EXEEXT'] + end + end + + def self.mac? + @mac = begin + if host_os =~ /darwin/ + true + else + false + end + end + end + + def self.osx? + mac? + end + + def self.x? + mac? + end + + # amount of memory the current process "is using", in RAM + # (doesn't include any swap memory that it may be using, just that in actual RAM) + # raises 'unknown' on jruby currently + def self.rss_bytes + # attempt to do this in a jruby friendly way + if OS::Underlying.windows? + # MRI, Java, IronRuby, Cygwin + if OS.java? + # no win32ole on 1.5.x, so leave here for compatibility...maybe for awhile :P + require 'java' + mem_bean = java.lang.management.ManagementFactory.getMemoryMXBean + mem_bean.heap_memory_usage.used + mem_bean.non_heap_memory_usage.used + else + wmi = nil + begin + require 'win32ole' + wmi = WIN32OLE.connect('winmgmts://') + rescue LoadError, NoMethodError => e # NoMethod for IronRuby currently [sigh] + raise 'rss unknown for this platform ' + e.to_s + end + processes = wmi.ExecQuery("select * from win32_process where ProcessId = #{Process.pid}") + memory_used = nil + # only allow for one process... + processes.each do |process| + raise 'multiple processes same pid?' if memory_used + memory_used = process.WorkingSetSize.to_i + end + memory_used + end + elsif OS.posix? # linux [though I've heard it works in OS X] + `ps -o rss= -p #{Process.pid}`.to_i * 1024 # in kiloBytes + else + raise 'unknown rss for this platform' + end + end + + class Underlying + def self.bsd? + OS.osx? + end + + def self.windows? + ENV['OS'] == 'Windows_NT' + end + + def self.linux? + OS.host_os =~ /linux/ ? true : false + end + + def self.docker? + system('grep -q docker /proc/self/cgroup') if OS.linux? + end + end + + def self.cygwin? + @cygwin = begin + if RUBY_PLATFORM =~ /-cygwin/ + true + else + false + end + end + end + + def self.dev_null # File::NULL in 1.9.3+ + @dev_null ||= begin + if OS.windows? + 'NUL' + else + '/dev/null' + end + end + end + + # provides easy way to see the relevant config entries + def self.report + relevant_keys = [ + 'arch', + 'host', + 'host_cpu', + 'host_os', + 'host_vendor', + 'target', + 'target_cpu', + 'target_os', + 'target_vendor' + ] + RbConfig::CONFIG.reject { |key, _val| !relevant_keys.include? key }.merge('RUBY_PLATFORM' => RUBY_PLATFORM).to_yaml + end + + def self.cpu_count + @cpu_count ||= + case RUBY_PLATFORM + when /darwin9/ + `hwprefs cpu_count`.to_i + when /darwin10/ + (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i + when /linux/ + `cat /proc/cpuinfo | grep processor | wc -l`.to_i + when /freebsd/ + `sysctl -n hw.ncpu`.to_i + else + if RbConfig::CONFIG['host_os'] =~ /darwin/ + (hwprefs_available? ? `hwprefs thread_count` : `sysctl -n hw.ncpu`).to_i + elsif windows? + # ENV counts hyper threaded...not good. + # out = ENV['NUMBER_OF_PROCESSORS'].to_i + require 'win32ole' + wmi = WIN32OLE.connect('winmgmts://') + cpu = wmi.ExecQuery('select NumberOfCores from Win32_Processor') # don't count hyper-threaded in this + cpu.to_enum.first.NumberOfCores + else + raise 'unknown platform processor_count' + end + end + end + + def self.open_file_command + if OS.cygwin? + 'cygstart' + elsif OS.doze? + 'start' + elsif OS.mac? + 'open' + else + 'xdg-open' + end + end + + def self.app_config_path(name) + if OS.doze? + return File.join(ENV['LOCALAPPDATA'], name) if ENV['LOCALAPPDATA'] + + File.join ENV['USERPROFILE'], 'Local Settings', 'Application Data', name + elsif OS.mac? + File.join ENV['HOME'], 'Library', 'Application Support', name + else + return File.join(ENV['XDG_CONFIG_HOME'], name) if ENV['XDG_CONFIG_HOME'] + + File.join ENV['HOME'], '.config', name + end + end + + def self.parse_os_release + raise "File /etc/os-release doesn't exists or not Linux" unless OS.linux? && File.exist?('/etc/os-release') + + output = {} + File.read('/etc/os-release').each_line do |line| + parsed_line = line.chomp.tr('"', '').split('=') + next if parsed_line.empty? + output[parsed_line[0].to_sym] = parsed_line[1] + end + output + end + + class << self + alias doze? windows? # a joke name but I use it and like it :P + alias jruby? java? + + # delegators for relevant config values + ['host', 'host_cpu', 'host_os'].each do |method_name| + define_method(method_name) { config[method_name] } + end + + private + + def hwprefs_available? + `which hwprefs` != '' + end + end +end diff --git a/os.gemspec b/os.gemspec index 6c2cdd6..b7ebcd2 100644 --- a/os.gemspec +++ b/os.gemspec @@ -1,3 +1,4 @@ +# encoding: utf-8 # Generated by jeweler # DO NOT EDIT THIS FILE DIRECTLY # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec' @@ -5,31 +6,32 @@ # stub: os 1.1.4 ruby lib Gem::Specification.new do |s| - s.name = "os".freeze - s.version = "1.1.4" + s.name = 'os'.freeze + s.version = '1.1.4' - s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version= - s.require_paths = ["lib".freeze] - s.authors = ["rdp".freeze, "David McCullars".freeze] - s.date = "2021-11-07" - s.description = "The OS gem allows for some useful and easy functions, like OS.windows? (=> true or false) OS.bits ( => 32 or 64) etc\"".freeze - s.email = "rogerpack2005@gmail.com".freeze + s.required_rubygems_version = Gem::Requirement.new('>= 0'.freeze) if s.respond_to? :required_rubygems_version= + s.require_paths = ['lib'.freeze] + s.authors = ['rdp'.freeze, 'David McCullars'.freeze] + s.date = '2021-11-07' + s.description = 'The OS gem allows for some useful and easy functions, like OS.windows? (=> true or false) OS.bits ( => 32 or 64) etc"'.freeze + s.email = 'rogerpack2005@gmail.com'.freeze s.files = [ - "ChangeLog", - "Gemfile", - "LICENSE", - "README.md", - "Rakefile", - "lib/os.rb", - "os.gemspec", + 'ChangeLog', + 'Gemfile', + 'LICENSE', + 'README.md', + 'Rakefile', + 'lib/os.rb', + 'os.gemspec' ] - s.homepage = "https://github.com/rdp/os".freeze - s.licenses = ["MIT".freeze] - s.rubygems_version = "2.7.6".freeze + s.homepage = 'https://github.com/rdp/os'.freeze + s.licenses = ['MIT'.freeze] + s.rubygems_version = '2.7.6'.freeze s.summary = "Simple and easy way to know if you're on windows or not (reliably), as well as how many bits the OS is, etc.".freeze - s.add_development_dependency(%q.freeze, ["~> 10.5"]) - s.add_development_dependency(%q.freeze, ["~> 3.5"]) - s.add_development_dependency(%q.freeze, ["~> 3.12"]) - s.add_development_dependency(%q.freeze, ["~> 4.7"]) + s.add_development_dependency('guard-rspec'.freeze, ['~> 4.7']) + s.add_development_dependency('rake'.freeze, ['~> 10.5']) + s.add_development_dependency('rspec'.freeze, ['~> 3.12']) + s.add_development_dependency('rubocop-lts'.freeze, ['~> 2.0']) # For Ruby 1.8.7 compat + s.add_development_dependency('test-unit'.freeze, ['~> 3.5']) end diff --git a/spec/config/rspec/rspec_core.rb b/spec/config/rspec/rspec_core.rb index 10f8c98..3660b26 100644 --- a/spec/config/rspec/rspec_core.rb +++ b/spec/config/rspec/rspec_core.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 # frozen_string_literal: true RSpec.configure do |config| diff --git a/spec/linux_spec.rb b/spec/linux_spec.rb index 93ef7ab..c34e698 100644 --- a/spec/linux_spec.rb +++ b/spec/linux_spec.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 describe 'For Linux, (Ubuntu, Ubuntu 10.04 LTS) ' do before(:each) do allow(ENV).to receive(:[]).with('OS') @@ -18,7 +19,6 @@ it { is_expected.not_to be_mac } it { is_expected.not_to be_osx } it { is_expected.not_to be_windows } - end describe OS::Underlying do @@ -30,4 +30,3 @@ it { is_expected.not_to be_windows } end end - diff --git a/spec/os_spec.rb b/spec/os_spec.rb index 8ed97b0..1f47269 100644 --- a/spec/os_spec.rb +++ b/spec/os_spec.rb @@ -1,27 +1,27 @@ -describe "OS" do - - it "identifies whether windows? or posix?" do +# encoding: utf-8 +describe 'OS' do + it 'identifies whether windows? or posix?' do if ENV['OS'] == 'Windows_NT' - unless RUBY_PLATFORM =~ /cygwin/ - assert OS.windows? == true - assert OS.doze? == true - assert OS.posix? == false # can fail in error at times...I guess because some other spec has reset ENV on us... - else + if RUBY_PLATFORM =~ /cygwin/ assert OS::Underlying.windows? assert OS.windows? == false assert OS.posix? == true + else + assert OS.windows? == true + assert OS.doze? == true + assert OS.posix? == false # can fail in error at times...I guess because some other spec has reset ENV on us... end assert OS::Underlying.windows? - elsif [/linux/, /darwin/].any? {|posix_pattern| (RbConfig::CONFIG["host_os"] =~ posix_pattern) || RUBY_PLATFORM =~ posix_pattern } + elsif [/linux/, /darwin/].any? { |posix_pattern| (RbConfig::CONFIG['host_os'] =~ posix_pattern) || RUBY_PLATFORM =~ posix_pattern } assert OS.windows? == false assert OS.posix? == true assert !OS::Underlying.windows? else - skip "create test" + skip 'create test' end end - it "has a bits method" do + it 'has a bits method' do if RUBY_PLATFORM =~ /mingw32/ assert OS.bits == 32 elsif RUBY_PLATFORM =~ /64/ # linux... @@ -35,12 +35,11 @@ elsif RUBY_PLATFORM =~ /i386/ assert OS.bits == 32 else - skip "os bits not tested!" + RUBY_PLATFORM + ' ' + RbConfig::CONFIG['host_os'] + skip 'os bits not tested!' + RUBY_PLATFORM + ' ' + RbConfig::CONFIG['host_os'] end - end - it "should have an iron_ruby method" do + it 'should have an iron_ruby method' do if defined?(RUBY_ENGINE) && RUBY_ENGINE == 'ironruby' assert OS.iron_ruby? == true else @@ -56,7 +55,7 @@ end end - it "should have a ruby_bin method" do + it 'should have a ruby_bin method' do if OS.windows? assert OS.ruby_bin.include?('.exe') if OS.iron_ruby? @@ -71,10 +70,9 @@ if OS.java? assert OS.ruby_bin.include?('jruby') # I think end - end - it "should have a cygwin? method" do + it 'should have a cygwin? method' do if RUBY_PLATFORM =~ /cygwin/ assert OS.cygwin? == true else @@ -82,7 +80,7 @@ end end - it "should have a functional mac? method" do + it 'should have a functional mac? method' do if RUBY_PLATFORM =~ /darwin/ assert OS.mac? == true else @@ -94,21 +92,21 @@ end end - it "should have a way to get rss_bytes on each platform" do + it 'should have a way to get rss_bytes on each platform' do bytes = OS.rss_bytes assert bytes > 0 # should always be true assert bytes.is_a?(Numeric) # don't want strings from any platform... end - it "should tell you what the right /dev/null is" do + it 'should tell you what the right /dev/null is' do if OS.windows? - expect(OS.dev_null).to eq("NUL") + expect(OS.dev_null).to eq('NUL') else - expect(OS.dev_null).to eq("/dev/null") + expect(OS.dev_null).to eq('/dev/null') end end - it "should have a jruby method" do + it 'should have a jruby method' do if defined?(RUBY_DESCRIPTION) && RUBY_DESCRIPTION =~ /^(jruby|java)/ assert OS.jruby? else @@ -116,14 +114,14 @@ end end - it "has working cpu count method" do + it 'has working cpu count method' do cpu_count = OS.cpu_count assert cpu_count >= 1 # CPU count is usually either a power of 2 or an even number. - assert ((cpu_count & (cpu_count - 1)) == 0) || ((cpu_count % 2) == 0) + assert ((cpu_count & (cpu_count - 1)) == 0) || cpu_count.even? end - it "has working cpu count method with no env. variable" do + it 'has working cpu count method with no env. variable' do OS.instance_variable_set(:@cpu_count, nil) # reset it if OS.windows? ENV.delete('NUMBER_OF_PROCESSORS') @@ -131,17 +129,17 @@ end end - it "should have a start/open command helper" do + it 'should have a start/open command helper' do if OS.doze? - assert OS.open_file_command == "start" + assert OS.open_file_command == 'start' elsif OS.mac? - assert OS.open_file_command == "open" + assert OS.open_file_command == 'open' else - assert OS.open_file_command == "xdg-open" + assert OS.open_file_command == 'xdg-open' end end - it "should provide a path to directory for application config" do + it 'should provide a path to directory for application config' do allow(ENV).to receive(:[]) home = '/home/user' @@ -159,19 +157,17 @@ end end - it "should have a freebsd? method" do + it 'should have a freebsd? method' do if OS.host_os =~ /freebsd/ assert OS.freebsd? == true else assert OS.freebsd? == false end end - end -describe OS, "provides access to to underlying config values" do - - describe "#config, supplys the CONFIG hash" do +describe OS, 'provides access to to underlying config values' do + describe '#config, supplys the CONFIG hash' do subject { OS.config } specify { expect(subject).to be_a(Hash) } @@ -185,8 +181,8 @@ end end - describe "by providing a delegate method for relevant keys in RbConfig::CONFIG" do - %w(host host_cpu host_os).sort.each do |config_key| + describe 'by providing a delegate method for relevant keys in RbConfig::CONFIG' do + ['host', 'host_cpu', 'host_os'].sort.each do |config_key| it "should delegate '#{config_key}'" do expected = "TEST #{config_key}" expect(RbConfig::CONFIG).to receive(:[]).with(config_key).and_return(expected) diff --git a/spec/osx_spec.rb b/spec/osx_spec.rb index 04b7f4d..626f532 100644 --- a/spec/osx_spec.rb +++ b/spec/osx_spec.rb @@ -1,9 +1,10 @@ +# encoding: utf-8 describe 'For OSX (Snow Leopard, 10.6),' do before(:each) do allow(ENV).to receive(:[]).with('OS').and_return(nil) # Issues stubbing RUBY_PLATFORM, using RbConfig instead. # Kernel.stub!(:RUBY_PLATFORM => "x86_64-darwin10.6") - allow(RbConfig::CONFIG).to receive(:[]).with('host_os').and_return("darwin10.6.0") + allow(RbConfig::CONFIG).to receive(:[]).with('host_os').and_return('darwin10.6.0') allow(RbConfig::CONFIG).to receive(:[]).with('host_cpu').and_return('i386') end @@ -16,7 +17,6 @@ it { is_expected.to be_posix } it { is_expected.not_to be_windows } - end describe OS::Underlying do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 36cf044..b9fe043 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -1,3 +1,4 @@ +# encoding: utf-8 # Third Party Libraries require 'rspec'