gpt4 book ai didi

testing - 如何为 Homebrew 公式编写测试?

转载 作者:行者123 更新时间:2023-11-28 20:22:05 25 4
gpt4 key购买 nike

我制作了一个 Homebrew 配方,现在只能通过本地水龙头访问。我想向 homebrew-core 发送拉取请求。现在我需要为我的公式编写测试。如何根据下面的示例编写?

test do
output = shell_output("#{bin}/balance 2>&1", 64)
assert_match "this is balance #{version}", output
end

我的公式

#!/usr/bin/env ruby

def match
files = Dir.glob("*")

if ARGV.length == 0
puts "usage: match <keyword>"
return
end

files.each { |x|
if File.directory?(x)
puts "#{x}_ found directory"
puts "***"
next
end

found = false

File.open(x).each_line.with_index do |line, index|
if line.include? ARGV[0]
puts "#{x}_ #{index+1} #{line}"
found = true
end
end

puts "***" if found
}
end

match

brew 配方

class Match < Formula
desc "Browse all files inside any directory for a keyword"
homepage "https://github.com/aatalyk/homebrew-match"
url ""
sha256 ""

def install
bin.install "match"
end
end

最佳答案

Homebrew 公式中的 shell 命令测试通常遵循以下场景:

  1. 创建命令可用的上下文:git 存储库、目录层次结构、示例文件等。
  2. 运行命令
  3. 断言结果正确

在您的情况下,自 matchgrep -R -就像你可以创建一堆包含一些内容的文件,然后运行 ​​match <something>并确保它找到正确的文件。

您可以在测试中使用任何 Ruby 代码以及 Homebrew 实用程序,例如 shell_output("...command...")获取命令的输出。

这是您可以编写的测试示例:

class Match < Formula
# ...

test do
# Create two dummy files
(testpath/"file1").write "foo\nbar\nqux"
(testpath/"file2").write "bar\nabc"

# Ensure `match bar` finds both files
assert_match "file1_ 2 bar\n***\nfile2_ 1 bar",
shell_output("#{bin}/match bar")

# Ensure `match abc` finds the second file
assert_match "file2_ 2 abc", shell_output("#{bin}/match abc")

# Ensure `match idontmatchanything` doesn’t match any of the files
assert_not_match(/file[12]/,
shell_output("#{bin}/match idontmatchanything"))
end
end

assert_match "something", shell_output("command")确保 (1) command成功运行并且 (2) 其输出包含“something”。

关于testing - 如何为 Homebrew 公式编写测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50608871/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com