pax_global_header00006660000000000000000000000064141050212110014475gustar00rootroot0000000000000052 comment=84b2d354527a8ee497410c5af369f7c8a9916f64 tty-which-0.5.0/000077500000000000000000000000001410502121100134175ustar00rootroot00000000000000tty-which-0.5.0/.editorconfig000066400000000000000000000002261410502121100160740ustar00rootroot00000000000000root = true [*.rb] charset = utf-8 end_of_line = lf insert_final_newline = true indent_style = space indent_size = 2 trim_trailing_whitespace = true tty-which-0.5.0/.github/000077500000000000000000000000001410502121100147575ustar00rootroot00000000000000tty-which-0.5.0/.github/FUNDING.yml000066400000000000000000000000241410502121100165700ustar00rootroot00000000000000github: piotrmurach tty-which-0.5.0/.github/ISSUE_TEMPLATE/000077500000000000000000000000001410502121100171425ustar00rootroot00000000000000tty-which-0.5.0/.github/ISSUE_TEMPLATE/BUG_REPORT.md000066400000000000000000000007671410502121100212060ustar00rootroot00000000000000--- name: Bug report about: Report something not working correctly or as expected title: '' labels: bug assignees: '' --- ### Describe the problem A brief description of the issue. ### Steps to reproduce the problem ``` Your code here to reproduce the issue ``` ### Actual behaviour What happened? This could be a description, log output, error raised etc. ### Expected behaviour What did you expect to happen? ### Describe your environment * OS version: * Ruby version: * TTY::Which version: tty-which-0.5.0/.github/ISSUE_TEMPLATE/FEATURE_REQUEST.md000066400000000000000000000005641410502121100217740ustar00rootroot00000000000000--- name: Feature request about: Suggest new functionality title: '' labels: enhancement assignees: '' --- ### Describe the problem A brief description of the problem you're trying to solve. ### How would the new feature work? A short explanation of the new feature. ``` Example code that shows possible usage ``` ### Drawbacks Can you see any potential drawbacks? tty-which-0.5.0/.github/ISSUE_TEMPLATE/config.yml000066400000000000000000000002721410502121100211330ustar00rootroot00000000000000blank_issues_enabled: false contact_links: - name: TTY Community Discussions url: https://github.com/piotrmurach/tty/discussions about: Suggest ideas, ask and answer questions tty-which-0.5.0/.github/PULL_REQUEST_TEMPLATE.md000066400000000000000000000007621410502121100205650ustar00rootroot00000000000000### Describe the change What does this Pull Request do? ### Why are we doing this? Any related context as to why is this is a desirable change. ### Benefits How will the library improve? ### Drawbacks Possible drawbacks applying this change. ### Requirements - [ ] Tests written & passing locally? - [ ] Code style checked? - [ ] Rebased with `master` branch? - [ ] Documentation updated? - [ ] Changelog updated? tty-which-0.5.0/.github/workflows/000077500000000000000000000000001410502121100170145ustar00rootroot00000000000000tty-which-0.5.0/.github/workflows/ci.yml000066400000000000000000000027021410502121100201330ustar00rootroot00000000000000--- name: CI on: push: branches: - master paths-ignore: - "*.md" pull_request: branches: - master paths-ignore: - "*.md" jobs: tests: name: Ruby ${{ matrix.ruby }} runs-on: ${{ matrix.os }} strategy: fail-fast: false matrix: os: - ubuntu-latest ruby: - 2.3 - 2.4 - 2.5 - 2.6 - 3.0 - ruby-head - jruby-9.2.13.0 - jruby-head - truffleruby-head include: - ruby: 2.0 os: ubuntu-latest coverage: false bundler: 1 - ruby: 2.1 os: ubuntu-latest coverage: false bundler: 1 - ruby: 2.2 os: ubuntu-latest coverage: false bundler: 1 - ruby: 2.7 os: ubuntu-latest coverage: true bundler: latest env: COVERAGE: ${{ matrix.coverage }} COVERALLS_REPO_TOKEN: ${{ secrets.COVERALLS_REPO_TOKEN }} continue-on-error: ${{ endsWith(matrix.ruby, 'head') }} steps: - uses: actions/checkout@v2 - name: Set up Ruby uses: ruby/setup-ruby@v1 with: ruby-version: ${{ matrix.ruby }} bundler: ${{ matrix.bundler }} - name: Install dependencies run: bundle install --jobs 4 --retry 3 - name: Run tests run: bundle exec rake ci tty-which-0.5.0/.gitignore000066400000000000000000000002711410502121100154070ustar00rootroot00000000000000*.gem *.rbc .bundle .config .yardoc Gemfile.lock InstalledFiles _yardoc coverage doc/ lib/bundler/man pkg rdoc spec/reports test/tmp test/version_tmp tmp *.bundle *.so *.o *.a mkmf.log tty-which-0.5.0/.rspec000066400000000000000000000000511410502121100145300ustar00rootroot00000000000000--color --require spec_helper --warnings tty-which-0.5.0/.rubocop.yml000066400000000000000000000014361410502121100156750ustar00rootroot00000000000000AllCops: NewCops: enable Layout/FirstArrayElementIndentation: Enabled: false Layout/LineLength: Max: 82 Layout/SpaceInsideHashLiteralBraces: EnforcedStyle: no_space Metrics/AbcSize: Max: 35 Metrics/BlockLength: CountComments: true Max: 25 IgnoredMethods: [] Exclude: - "spec/**/*" Metrics/ClassLength: Max: 1500 Metrics/CyclomaticComplexity: Max: 10 Metrics/MethodLength: Max: 20 Metrics/PerceivedComplexity: Max: 10 Naming/FileName: Exclude: - "lib/tty-which.rb" Style/AsciiComments: Enabled: false Style/BlockDelimiters: Enabled: false Style/CommentedKeyword: Enabled: false Style/LambdaCall: EnforcedStyle: braces Style/StringLiterals: EnforcedStyle: double_quotes Style/StringLiteralsInInterpolation: EnforcedStyle: double_quotes tty-which-0.5.0/CHANGELOG.md000066400000000000000000000040641410502121100152340ustar00rootroot00000000000000# Change log ## [v0.5.0] - 2021-08-11 ### Changed * Change to use double-quoted strings * Remove bundler as development dependency ### Fixed * Fix to stop joining absolute path and extension with a file path separator ## [v0.4.2] - 2020-01-20 ### Changed * Change gemspec to add metadata and remove test artifacts ## [v0.4.1] - 2019-06-02 ### Changed * Change to relax bundler dependency version ## [v0.4.0] - 2018-10-13 ### Added * Add ability to specify search paths for #which and #exist? calls ### Changed * Change to freeze all strings * Change gemspec to require Ruby >= 2.0.0 * Change gemspec to load files without calling git * Change gemspec to add rspec as dev dependency ## [v0.3.0] - 2017-03-20 ### Changed * Change #extensions to use file path separator * Change files loading * Remove search paths caching ## [v0.2.2] - 2017-02-06 ### Fixed * Fix File namespacing issue ## [v0.2.1] - 2016-12-26 ### Changed * Change to stop shadowing path var in Which#search_paths ## [v0.2.0] - 2016-07-01 ### Added * Add Which#exist? to check if file exists based on found path ### Changed * Change Which#search_paths to allow for paths argument * Rename Which#executable_file_with_ext? to #file_with_exec_ext? * Rename Which#path_with_executable_file? to #file_with_path? ### Fixed * Fix bug with Which#file_with_exec_ext? when comparing extensions ## [v0.1.0] - 2015-05-30 * Initial implementation and release [v0.5.0]: https://github.com/piotrmurach/tty-which/compare/v0.4.2...v0.5.0 [v0.4.2]: https://github.com/piotrmurach/tty-which/compare/v0.4.1...v0.4.2 [v0.4.1]: https://github.com/piotrmurach/tty-which/compare/v0.4.0...v0.4.1 [v0.4.0]: https://github.com/piotrmurach/tty-which/compare/v0.3.0...v0.4.0 [v0.3.0]: https://github.com/piotrmurach/tty-which/compare/v0.2.2...v0.3.0 [v0.2.2]: https://github.com/piotrmurach/tty-which/compare/v0.2.1...v0.2.2 [v0.2.1]: https://github.com/piotrmurach/tty-which/compare/v0.2.0...v0.2.1 [v0.2.0]: https://github.com/piotrmurach/tty-which/compare/v0.1.0...v0.2.0 [v0.1.0]: https://github.com/piotrmurach/tty-which/compare/v0.1.0 tty-which-0.5.0/CODE_OF_CONDUCT.md000066400000000000000000000121431410502121100162170ustar00rootroot00000000000000# Contributor Covenant Code of Conduct ## Our Pledge We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation. We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community. ## Our Standards Examples of behavior that contributes to a positive environment for our community include: * Demonstrating empathy and kindness toward other people * Being respectful of differing opinions, viewpoints, and experiences * Giving and gracefully accepting constructive feedback * Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience * Focusing on what is best not just for us as individuals, but for the overall community Examples of unacceptable behavior include: * The use of sexualized language or imagery, and sexual attention or advances of any kind * Trolling, insulting or derogatory comments, and personal or political attacks * Public or private harassment * Publishing others' private information, such as a physical or email address, without their explicit permission * Other conduct which could reasonably be considered inappropriate in a professional setting ## Enforcement Responsibilities Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful. Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate. ## Scope This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. ## Enforcement Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at piotr@piotrmurach.com. All complaints will be reviewed and investigated promptly and fairly. All community leaders are obligated to respect the privacy and security of the reporter of any incident. ## Enforcement Guidelines Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct: ### 1. Correction **Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community. **Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested. ### 2. Warning **Community Impact**: A violation through a single incident or series of actions. **Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban. ### 3. Temporary Ban **Community Impact**: A serious violation of community standards, including sustained inappropriate behavior. **Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban. ### 4. Permanent Ban **Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals. **Consequence**: A permanent ban from any sort of public interaction within the community. ## Attribution This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html. Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity). [homepage]: https://www.contributor-covenant.org For answers to common questions about this code of conduct, see the FAQ at https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations. tty-which-0.5.0/Gemfile000066400000000000000000000004601410502121100147120ustar00rootroot00000000000000source "https://rubygems.org" gemspec gem "json", "2.4.1" if RUBY_VERSION == "2.0.0" group :test do if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new("2.5.0") gem "coveralls_reborn", "~> 0.22.0" gem "simplecov", "~> 0.21.0" end end group :metrics do gem "yardstick", "~> 0.9.9" end tty-which-0.5.0/LICENSE.txt000066400000000000000000000020771410502121100152500ustar00rootroot00000000000000Copyright (c) 2015 Piotr Murach (piotrmurach.com) MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tty-which-0.5.0/README.md000066400000000000000000000062341410502121100147030ustar00rootroot00000000000000
TTY Toolkit logo
# TTY::Which [![Gitter](https://badges.gitter.im/Join%20Chat.svg)][gitter] [![Gem Version](https://badge.fury.io/rb/tty-which.svg)][gem] [![Actions CI](https://github.com/piotrmurach/tty-which/workflows/CI/badge.svg?branch=master)][gh_actions_ci] [![Build status](https://ci.appveyor.com/api/projects/status/2rpm67huf1nh98d0?svg=true)][appveyor] [![Code Climate](https://codeclimate.com/github/piotrmurach/tty-which/badges/gpa.svg)][codeclimate] [![Coverage Status](https://coveralls.io/repos/github/piotrmurach/tty-which/badge.svg?branch=master)][coveralls] [![Inline docs](https://inch-ci.org/github/piotrmurach/tty-which.svg?branch=master)][inchpages] [gitter]: https://gitter.im/piotrmurach/tty [gem]: https://badge.fury.io/rb/tty-which [gh_actions_ci]: https://github.com/piotrmurach/tty-which/actions?query=workflow%3ACI [appveyor]: https://ci.appveyor.com/project/piotrmurach/tty-which [codeclimate]: https://codeclimate.com/github/piotrmurach/tty-which [coveralls]: https://coveralls.io/github/piotrmurach/tty-which [inchpages]: https://inch-ci.org/github/piotrmurach/tty-which > Platform independent implementation of Unix `which` utility that searches for executable file in the path variable. **TTY::Which** provides cross-platform executables search component for [TTY](https://github.com/piotrmurach/tty) toolkit. ## Installation Add this line to your application's Gemfile: gem "tty-which" And then execute: $ bundle Or install it yourself as: $ gem install tty-which ## Usage **TTY::Which** has `which` method that searches set of directories for an executable file based on the `PATH` environment variable. When the path to an executable program exists, an absolute path is returned, otherwise `nil`. For example, to find location for an executable program do: ```ruby TTY::Which.which("less") # => "/usr/bin/less" TTY::Which.which("git") # => "C:\Program Files\Git\bin\git" ``` You can also check an absolute path to executable: ```ruby TTY::Which.which("/usr/bin/ruby") # => "/usr/bin/ruby" ``` You can also specify directly the paths to search using `:paths` keyword: ```ruby TTY::Which.which("ruby", paths: ["/usr/local/bin", "/usr/bin", "/bin"]) # => "/usr/local/bin/ruby" ``` When you're only interesting in knowing that an executable exists on the system use the `exist?` call: ```ruby TTY::Which.exist?("ruby") # => true ``` ## Contributing Bug reports and pull requests are welcome on GitHub at https://github.com/piotrmurach/tty-which. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct. 1. Fork it ( https://github.com/piotrmurach/tty-which/fork ) 2. Create your feature branch (`git checkout -b my-new-feature`) 3. Commit your changes (`git commit -am 'Add some feature'`) 4. Push to the branch (`git push origin my-new-feature`) 5. Create a new Pull Request ## Copyright Copyright (c) 2015 Piotr Murach. See LICENSE for further details. tty-which-0.5.0/Rakefile000066400000000000000000000002541410502121100150650ustar00rootroot00000000000000# frozen_string_literal: true require "bundler/gem_tasks" FileList["tasks/**/*.rake"].each(&method(:import)) desc "Run all specs" task ci: %w[spec] task default: :spec tty-which-0.5.0/appveyor.yml000066400000000000000000000011461410502121100160110ustar00rootroot00000000000000--- install: - SET PATH=C:\Ruby%ruby_version%\bin;%PATH% - gem install bundler -v '< 2.0' - bundle install before_test: - ruby -v - gem -v - bundle -v build: off test_script: - bundle exec rake ci environment: matrix: - ruby_version: "200" - ruby_version: "200-x64" - ruby_version: "21" - ruby_version: "21-x64" - ruby_version: "22" - ruby_version: "22-x64" - ruby_version: "23" - ruby_version: "23-x64" - ruby_version: "24" - ruby_version: "24-x64" - ruby_version: "25" - ruby_version: "25-x64" - ruby_version: "26" - ruby_version: "26-x64" tty-which-0.5.0/lib/000077500000000000000000000000001410502121100141655ustar00rootroot00000000000000tty-which-0.5.0/lib/tty-which.rb000066400000000000000000000000741410502121100164330ustar00rootroot00000000000000# frozen_string_literal: true require_relative "tty/which" tty-which-0.5.0/lib/tty/000077500000000000000000000000001410502121100150055ustar00rootroot00000000000000tty-which-0.5.0/lib/tty/which.rb000066400000000000000000000105151410502121100164360ustar00rootroot00000000000000# frozen_string_literal: true require_relative "which/version" module TTY # A class responsible for finding an executable in the PATH module Which # Find an executable in a platform independent way # # @param [String] cmd # the command to search for # @param [Array] paths # the paths to look through # # @example # which("ruby") # => "/usr/local/bin/ruby" # which("/usr/local/bin/ruby") # => "/usr/local/bin/ruby" # which("foo") # => nil # # @example # which("ruby", paths: ["/usr/locale/bin", "/usr/bin", "/bin"]) # # @return [String, nil] # the absolute path to executable if found, `nil` otherwise # # @api public def which(cmd, paths: search_paths) if file_with_path?(cmd) return cmd if executable_file?(cmd) extensions.each do |ext| exe = "#{cmd}#{ext}" return ::File.absolute_path(exe) if executable_file?(exe) end return nil end paths.each do |path| if file_with_exec_ext?(cmd) exe = ::File.join(path, cmd) return ::File.absolute_path(exe) if executable_file?(exe) end extensions.each do |ext| exe = ::File.join(path, "#{cmd}#{ext}") return ::File.absolute_path(exe) if executable_file?(exe) end end nil end module_function :which # Check if executable exists in the path # # @param [String] cmd # the executable to check # # @param [Array] paths # paths to check # # @return [Boolean] # # @api public def exist?(cmd, paths: search_paths) !which(cmd, paths: paths).nil? end module_function :exist? # Find default system paths # # @param [String] path # the path to search through # # @example # search_paths("/usr/local/bin:/bin") # # => ["/bin"] # # @return [Array] # the array of paths to search # # @api private def search_paths(path = ENV["PATH"]) paths = if path && !path.empty? path.split(::File::PATH_SEPARATOR) else %w[/usr/local/bin /usr/ucb /usr/bin /bin] end paths.select(&Dir.method(:exist?)) end module_function :search_paths # All possible file extensions # # @example # extensions(".exe;cmd;.bat") # # => [".exe", ".bat"] # # @param [String] path_ext # a string of semicolon separated filename extensions # # @return [Array] # an array with valid file extensions # # @api private def extensions(path_ext = ENV["PATHEXT"]) return [""] unless path_ext path_ext.split(::File::PATH_SEPARATOR).select { |part| part.include?(".") } end module_function :extensions # Determines if filename is an executable file # # @example Basic usage # executable_file?("/usr/bin/less") # => true # # @example Executable in directory # executable_file?("less", "/usr/bin") # => true # executable_file?("less", "/usr") # => false # # @param [String] filename # the path to file # @param [String] dir # the directory within which to search for filename # # @return [Boolean] # # @api private def executable_file?(filename, dir = nil) path = ::File.join(dir, filename) if dir path ||= filename ::File.file?(path) && ::File.executable?(path) end module_function :executable_file? # Check if command itself has executable extension # # @param [String] filename # the path to executable file # # @example # file_with_exec_ext?("file.bat") # # => true # # @return [Boolean] # # @api private def file_with_exec_ext?(filename) extension = ::File.extname(filename) return false if extension.empty? extensions.any? { |ext| extension.casecmp(ext).zero? } end module_function :file_with_exec_ext? # Check if executable file is part of absolute/relative path # # @param [String] cmd # the executable to check # # @return [Boolean] # # @api private def file_with_path?(cmd) ::File.expand_path(cmd) == cmd end module_function :file_with_path? end # Which end # TTY tty-which-0.5.0/lib/tty/which/000077500000000000000000000000001410502121100161075ustar00rootroot00000000000000tty-which-0.5.0/lib/tty/which/version.rb000066400000000000000000000001471410502121100201230ustar00rootroot00000000000000# frozen_string_literal: true module TTY module Which VERSION = "0.5.0" end # Which end # TTY tty-which-0.5.0/spec/000077500000000000000000000000001410502121100143515ustar00rootroot00000000000000tty-which-0.5.0/spec/spec_helper.rb000066400000000000000000000020041410502121100171630ustar00rootroot00000000000000# frozen_string_literal: true if ENV["COVERAGE"] == "true" require "simplecov" require "coveralls" SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter.new([ SimpleCov::Formatter::HTMLFormatter, Coveralls::SimpleCov::Formatter ]) SimpleCov.start do command_name "spec" add_filter "spec" end end require "tty-which" RSpec.configure do |config| config.expect_with :rspec do |expectations| expectations.include_chain_clauses_in_custom_matcher_descriptions = true end config.mock_with :rspec do |mocks| mocks.verify_partial_doubles = true end # Limits the available syntax to the non-monkey patched syntax that is recommended. config.disable_monkey_patching! # This setting enables warnings. It's recommended, but in some cases may # be too noisy due to issues in dependencies. config.warnings = true if config.files_to_run.one? config.default_formatter = "doc" end config.profile_examples = 2 config.order = :random Kernel.srand config.seed end tty-which-0.5.0/spec/unit/000077500000000000000000000000001410502121100153305ustar00rootroot00000000000000tty-which-0.5.0/spec/unit/executable_file_spec.rb000066400000000000000000000015761410502121100220200ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#executable_file?" do it "checks if file in directory is executable" do path = "/usr/local/bin/ruby" allow(::File).to receive(:join).and_call_original allow(::File).to receive(:file?).and_call_original allow(::File).to receive(:join).with("/usr/local/bin", "ruby").and_return(path) allow(::File).to receive(:file?).with(path).and_return(true) allow(::File).to receive(:executable?).with(path).and_return(true) expect(TTY::Which.executable_file?("ruby", "/usr/local/bin")).to eq(true) end it "checks if only a file is executable" do allow(::File).to receive(:file?).and_call_original allow(::File).to receive(:file?).with("ruby").and_return(true) allow(::File).to receive(:executable?).with("ruby").and_return(true) expect(TTY::Which.executable_file?("ruby")).to eql(true) end end tty-which-0.5.0/spec/unit/exist_spec.rb000066400000000000000000000010701410502121100200210ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#exist?" do it "finds executable in the path" do allow(TTY::Which).to receive(:which).with("ruby", a_hash_including(:paths)) .and_return("/usr/loca/bin/ruby") expect(TTY::Which.exist?("ruby")).to be(true) end it "fails to find executable in the path" do allow(TTY::Which).to receive(:which).with("ruby", a_hash_including(:paths)) .and_return(nil) expect(TTY::Which.exist?("ruby")).to be(false) end end tty-which-0.5.0/spec/unit/extensions_spec.rb000066400000000000000000000007561410502121100210760ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#extensions" do it "provides extensions" do exts = [".COM", ".EXE", ".BAT", ".CMD", ".VBS", ".RB", ".RBW"] exts_path = exts.join(::File::PATH_SEPARATOR) allow(ENV).to receive(:[]).with("PATHEXT").and_return(exts_path) expect(TTY::Which.extensions).to eq(exts) end it "finds no extensions" do allow(ENV).to receive(:[]).with("PATHEXT").and_return(nil) expect(TTY::Which.extensions).to eq([""]) end end tty-which-0.5.0/spec/unit/file_with_exec_ext_spec.rb000066400000000000000000000010261410502121100225240ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#file_with_exec_ext?" do it "detects executable extension" do filename = "file.exe" allow(TTY::Which).to receive(:extensions).and_return([".EXE", ".BAT", ".CMD"]) expect(TTY::Which.file_with_exec_ext?(filename)).to eq(true) end it "fails to detect executable extension" do filename = "file.unknown" allow(TTY::Which).to receive(:extensions).and_return([".EXE", ".BAT"]) expect(TTY::Which.file_with_exec_ext?(filename)).to eq(false) end end tty-which-0.5.0/spec/unit/search_paths_spec.rb000066400000000000000000000016561410502121100213430ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#search_paths" do it "defauls search paths" do allow(ENV).to receive(:[]).with("PATH").and_return([]) allow(Dir).to receive(:exist?).and_return(true) expect(TTY::Which.search_paths).to eq([ "/usr/local/bin", "/usr/ucb", "/usr/bin", "/bin" ]) end it "finds paths in path environment" do paths = ["/bin", "/usr/bin", "/usr/local/bin", "/opt/local/bin"] path = paths.join(::File::PATH_SEPARATOR) allow(ENV).to receive(:[]).with("PATH").and_return(path) allow(Dir).to receive(:exist?).and_return(true) expect(TTY::Which.search_paths).to eq(paths) end it "accepts paths to search as an argument" do paths = ["/bin", "/usr/bin", "/usr/local/bin", "/opt/local/bin"] path = paths.join(::File::PATH_SEPARATOR) allow(Dir).to receive(:exist?).and_return(true) expect(TTY::Which.search_paths(path)).to eq(paths) end end tty-which-0.5.0/spec/unit/which_spec.rb000066400000000000000000000123421410502121100177730ustar00rootroot00000000000000# frozen_string_literal: true RSpec.describe TTY::Which, "#which" do before { stub_const("Which", described_class) } context "without extension" do let(:path) { %w[/bin /usr/bin /usr/local/bin /opt/local/bin].join(":") } let(:cmds) { %w[/usr/bin/ls /bin/sh /usr/bin/ruby /usr/local/git/bin/git] } before do allow(ENV).to receive(:[]).with("PATHEXT").and_return(nil) allow(ENV).to receive(:[]).with("PATH").and_return(path) stub_const("::File::PATH_SEPARATOR", ":") stub_const("::File::SEPARATOR", "/") allow(Dir).to receive(:exist?) { true } end it "handles path with executable file /bin/sh" do allow(Which).to receive(:file_with_path?) { true } allow(Which).to receive(:executable_file?) { true } expect(Which.which("/bin/sh")).to eq("/bin/sh") end it "fails to find path executable" do allow(Which).to receive(:file_with_path?) { true } allow(Which).to receive(:executable_file?) { false } expect(Which.which("/bin/sh")).to eq(nil) end it "searches executable file git" do dir_path = "/usr/local/bin" cmd = "git" expected_path = "#{dir_path}/#{cmd}" allow(Which).to receive(:file_with_path?) { false } allow(Which).to receive(:file_with_exec_ext?) { false } allow(::File).to receive(:join).and_call_original allow(::File).to receive(:join).with(dir_path, cmd) .and_return(expected_path) allow(Which).to receive(:executable_file?) { false } allow(Which).to receive(:executable_file?).with(expected_path) { true } allow(::File).to receive(:absolute_path).with(expected_path) .and_return(expected_path) expect(Which.which(cmd)).to eq(expected_path) end it "allows to search through custom paths" do paths = %w[/usr/local/bin /usr/bin /bin] allow(Which).to receive(:executable_file?).with("/usr/local/bin/ruby") { false } allow(Which).to receive(:executable_file?).with("/usr/bin/ruby") { true } allow(::File).to receive(:absolute_path).with("/usr/bin/ruby") .and_return("/usr/bin/ruby") expect(TTY::Which.which("ruby", paths: paths)).to eq("/usr/bin/ruby") end end context "with extension" do let(:path) { ["C:\\Program Files\\Git\\bin"].join(";") } let(:exts) { %w[.msi .exe .bat .cmd].join(";") } before do allow(ENV).to receive(:[]).with("PATHEXT").and_return(exts) allow(ENV).to receive(:[]).with("PATH").and_return(path) stub_const("::File::PATH_SEPARATOR", ";") stub_const("::File::SEPARATOR", "\\") allow(Dir).to receive(:exist?) { true } end it "handles path with executable file C:\\Program Files\\Git\\bin\\git" do allow(Which).to receive(:file_with_path?) { true } allow(Which).to receive(:executable_file?).with(any_args) { false } path_with_exe_file = "C:\\Program Files\\Git\\bin\\git" expected_path = "#{path_with_exe_file}.exe" allow(Which).to receive(:executable_file?).with(expected_path) { true } allow(::File).to receive(:absolute_path).and_return(expected_path) expect(Which.which(path_with_exe_file)).to eq(expected_path) expect(Which).to have_received(:executable_file?) .with("#{path_with_exe_file}.msi") end it "searches path for executable git.exe" do dir_path = "C:\\Program Files\\Git\\bin" cmd = "git.exe" expected_path = "#{dir_path}\\#{cmd}" allow(Which).to receive(:file_with_path?) { false } allow(Which).to receive(:file_with_exec_ext?).with(cmd) { true } allow(::File).to receive(:join).and_call_original allow(::File).to receive(:join).with(dir_path, any_args) allow(::File).to receive(:join).with(dir_path, cmd) .and_return(expected_path) allow(Which).to receive(:executable_file?).with(any_args) { false } allow(Which).to receive(:executable_file?).with(expected_path) { true } allow(::File).to receive(:absolute_path).with(expected_path) .and_return(expected_path) expect(Which.which(cmd)).to eq(expected_path) expect(::File).to have_received(:absolute_path).with(expected_path) end it "searches path for executable git" do dir_path = "C:\\Program Files\\Git\\bin" cmd = "git" expected_path = "#{dir_path}\\#{cmd}.exe" allow(Which).to receive(:file_with_path?) { false } allow(Which).to receive(:file_with_exec_ext?).with(cmd) { false } allow(::File).to receive(:join).and_call_original allow(::File).to receive(:join).with(dir_path, any_args) allow(::File).to receive(:join).with(dir_path, "#{cmd}.exe") .and_return(expected_path) allow(Which).to receive(:executable_file?).with(any_args) { false } allow(Which).to receive(:executable_file?).with(expected_path) { true } allow(::File).to receive(:absolute_path).with(expected_path) .and_return(expected_path) expect(Which.which(cmd)).to eq(expected_path) expect(::File).to have_received(:absolute_path).with(expected_path) end end end tty-which-0.5.0/tasks/000077500000000000000000000000001410502121100145445ustar00rootroot00000000000000tty-which-0.5.0/tasks/console.rake000066400000000000000000000003251410502121100170520ustar00rootroot00000000000000# frozen_string_literal: true desc "Load gem inside irb console" task :console do require "irb" require "irb/completion" require_relative "../lib/tty-which" ARGV.clear IRB.start end task c: %w[console] tty-which-0.5.0/tasks/coverage.rake000066400000000000000000000003361410502121100172050ustar00rootroot00000000000000# frozen_string_literal: true desc "Measure code coverage" task :coverage do begin original, ENV["COVERAGE"] = ENV["COVERAGE"], "true" Rake::Task["spec"].invoke ensure ENV["COVERAGE"] = original end end tty-which-0.5.0/tasks/spec.rake000066400000000000000000000014741410502121100163500ustar00rootroot00000000000000# frozen_string_literal: true begin require "rspec/core/rake_task" desc "Run all specs" RSpec::Core::RakeTask.new(:spec) do |task| task.pattern = "spec/{unit,integration}{,/*/**}/*_spec.rb" end namespace :spec do desc "Run unit specs" RSpec::Core::RakeTask.new(:unit) do |task| task.pattern = "spec/unit{,/*/**}/*_spec.rb" end desc "Run integration specs" RSpec::Core::RakeTask.new(:integration) do |task| task.pattern = "spec/integration{,/*/**}/*_spec.rb" end desc "Run performance specs" RSpec::Core::RakeTask.new(:perf) do |task| task.pattern = "spec/perf{,/*/**}/*_spec.rb" end end rescue LoadError %w[spec spec:unit spec:integration].each do |name| task name do warn "In order to run #{name}, do `gem install rspec`" end end end tty-which-0.5.0/tty-which.gemspec000066400000000000000000000023561410502121100167120ustar00rootroot00000000000000# frozen_string_literal: true require_relative "lib/tty/which/version" Gem::Specification.new do |spec| spec.name = "tty-which" spec.version = TTY::Which::VERSION spec.authors = ["Piotr Murach"] spec.email = ["piotr@piotrmurach.com"] spec.summary = "Platform independent implementation of Unix which command." spec.description = "Platform independent implementation of Unix which command." spec.homepage = "https://ttytoolkit.org" spec.license = "MIT" spec.metadata = { "allowed_push_host" => "https://rubygems.org", "bug_tracker_uri" => "https://github.com/piotrmurach/tty-which/issues", "changelog_uri" => "https://github.com/piotrmurach/tty-which/blob/master/CHANGELOG.md", "documentation_uri" => "https://www.rubydoc.info/gems/tty-which", "homepage_uri" => spec.homepage, "source_code_uri" => "https://github.com/piotrmurach/tty-which" } spec.files = Dir["lib/**/*"] spec.extra_rdoc_files = Dir["README.md", "CHANGELOG.md", "LICENSE.txt"] spec.bindir = "exe" spec.require_paths = ["lib"] spec.required_ruby_version = ">= 2.0.0" spec.add_development_dependency "rake" spec.add_development_dependency "rspec", ">= 3.0" end