【问题标题】:Manually testing a Ruby CLI手动测试 Ruby CLI
【发布时间】:2017-12-21 20:05:44
【问题描述】:

我正在使用 Thor 使用 Ruby 构建 CLI gem。我运行rake install 运行rake build 然后在本地安装gem 的任务。但是,当我尝试在命令行中运行它时,它找不到该命令。 gem 被称为smokestack,所以理论上我应该一旦安装它就可以在终端中运行它。

结构:

├── CODE_OF_CONDUCT.md
├── Gemfile
├── Gemfile.lock
├── LICENSE.txt
├── README.md
├── Rakefile
├── bin
│   ├── console
│   ├── setup
│   └── smokestack
├── lib
│   ├── smokestack
│   │   ├── build.rb
│   │   ├── cli.rb
│   │   └── version.rb
│   └── smokestack.rb
├── pkg
│   └── smokestack-0.1.0.gem
├── smokestack.gemspec
└── test
    ├── build_test.rb
    ├── smokestack_test.rb
    └── test_helper.rb

bin/smokestack:

#!/usr/bin/env ruby -wU

require 'smokestack'

Smokestack::Cli.start(ARGV)

您可以在树中看到我运行 rake install 时的 pkg 文件夹。 这是我运行它时的结果:

smokestack 0.1.0 built to pkg/smokestack-0.1.0.gem.
smokestack (0.1.0) installed. 

然后我在终端中运行smokestack 并得到错误:zsh: command not found: smokestack

我也试过gem install --local ~/path/to/gem/pkg/gem.gem 结果:

Successfully installed smokestack-0.1.0
Parsing documentation for smokestack-0.1.0
Done installing documentation for smokestack after 0 seconds
1 gem installed

这会导致相同的“找不到命令错误”。

问题我应该如何在本地运行此 CLI 以在开发期间对其进行测试?

理想情况下,它会与其所在的当前项目进行交互,因此仅在其自己的目录中运行 bundle exec bin/smokestack 不会产生我需要的正确结果。无论如何,它应该是一个系统 CLI 对吧?

Here is the repo 如有必要,请提供其他上下文。

【问题讨论】:

    标签: ruby rubygems command-line-interface


    【解决方案1】:

    这有几件事出了问题。首先,bundle 将可执行文件从/bin 移出到/exe。参考【这篇文章(http://bundler.io/blog/2015/03/20/moving-bins-to-exe.html)】。

    我将bin/smokestack 移至exe/smokestack

    您还必须确保并暂存您的文件,因为 .gemspec 文件通过运行 git ls-files 获取 gem 文件列表。现在一切都完成了,一切似乎都在工作。

    【讨论】:

      猜你喜欢
      • 2014-11-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-06
      • 1970-01-01
      • 1970-01-01
      • 2022-11-17
      • 2018-04-25
      相关资源
      最近更新 更多