Skip to content

Commit

Permalink
#835 test_log renamed to fake_log
Browse files Browse the repository at this point in the history
  • Loading branch information
davvd committed Jul 15, 2024
1 parent 30a7fde commit 1f2cebc
Show file tree
Hide file tree
Showing 55 changed files with 291 additions and 280 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ gem 'minitest-fail-fast', '0.1.0', require: false
gem 'minitest-hooks', '1.5.1', require: false
gem 'minitest-reporters', '1.6.1', require: false
gem 'rake', '13.1.0', require: false
gem 'random-port', '0.6.0', require: false
gem 'random-port', '~>0', require: false
gem 'rdoc', '6.6.0', require: false
gem 'rspec-rails', '6.1.0', require: false
gem 'rubocop', '1.57.2', require: false
Expand Down
2 changes: 1 addition & 1 deletion fixtures/scripts/_head.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export RUBYOPT="-W0"
alias zold="$1 --ignore-this-stupid-option --halt-code=test --ignore-global-config --trace --network=test --no-colors --dump-errors"

function reserve_port {
python -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'
python -c 'import socket; s=socket.socket(); s.bind(("127.0.0.1", 0)); print(s.getsockname()[1]); s.close()'
}

function wait_for_url {
Expand Down
4 changes: 2 additions & 2 deletions test/commands/routines/test_audit.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@
# License:: MIT
class TestAudit < Zold::Test
def test_audits
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
opts = { 'routine-immediately' => true }
routine = Zold::Routines::Audit.new(opts, home.wallets, log: test_log)
routine = Zold::Routines::Audit.new(opts, home.wallets, log: fake_log)
routine.exec
end
end
Expand Down
12 changes: 6 additions & 6 deletions test/commands/routines/test_gc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,37 +32,37 @@
# License:: MIT
class TestGc < Zold::Test
def test_collects_garbage
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallets = home.wallets
home.create_wallet
opts = { 'routine-immediately' => true, 'gc-age' => 0 }
assert_equal(1, wallets.count)
routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
routine = Zold::Routines::Gc.new(opts, wallets, log: fake_log)
routine.exec
assert_equal(0, wallets.count)
end
end

def test_doesnt_touch_non_empty_wallets
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallets = home.wallets
wallet = home.create_wallet
amount = Zold::Amount.new(zld: 39.99)
key = Zold::Key.new(file: 'fixtures/id_rsa')
wallet.sub(amount, "NOPREFIX@#{Zold::Id.new}", key)
opts = { 'routine-immediately' => true, 'gc-age' => 0 }
routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
routine = Zold::Routines::Gc.new(opts, wallets, log: fake_log)
routine.exec
assert_equal(1, wallets.count)
end
end

def test_doesnt_touch_fresh_wallets
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallets = home.wallets
home.create_wallet
opts = { 'routine-immediately' => true, 'gc-age' => 60 * 60 }
routine = Zold::Routines::Gc.new(opts, wallets, log: test_log)
routine = Zold::Routines::Gc.new(opts, wallets, log: fake_log)
routine.exec
assert_equal(1, wallets.count)
end
Expand Down
4 changes: 2 additions & 2 deletions test/commands/routines/test_reconcile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
# License:: MIT
class TestReconcile < Zold::Test
def test_reconciles
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
remotes = home.remotes
remotes.clean
remotes.masters
Expand All @@ -43,7 +43,7 @@ def test_reconciles
stub_request(:get, "http://#{m[:host]}:#{m[:port]}/wallet/#{Zold::Id::ROOT}").to_return(status: 404)
opts = { 'never-reboot' => true, 'routine-immediately' => true }
routine = Zold::Routines::Reconcile.new(
opts, home.wallets, remotes, home.copies.root, 'some-fake-host:2096', log: test_log
opts, home.wallets, remotes, home.copies.root, 'some-fake-host:2096', log: fake_log
)
routine.exec
end
Expand Down
2 changes: 1 addition & 1 deletion test/commands/routines/test_reconnect.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def test_reconnects
remotes.add('localhost', 4096)
stub_request(:get, 'http://localhost:4096/remotes').to_return(status: 404)
opts = { 'never-reboot' => true, 'routine-immediately' => true }
routine = Zold::Routines::Reconnect.new(opts, remotes, log: test_log)
routine = Zold::Routines::Reconnect.new(opts, remotes, log: fake_log)
routine.exec
end
end
Expand Down
2 changes: 1 addition & 1 deletion test/commands/routines/test_retire.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
class TestRetire < Zold::Test
def test_retires
opts = { 'never-reboot' => false, 'routine-immediately' => true }
routine = Zold::Routines::Retire.new(opts, log: test_log)
routine = Zold::Routines::Retire.new(opts, log: fake_log)
routine.exec(10 * 24 * 60)
end
end
12 changes: 6 additions & 6 deletions test/commands/test_alias.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ class TestAlias < Zold::Test
# The syntax is already documented in the alias command in the help.
def test_set_writes_alias_to_the_alias_file
skip
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
Zold::Alias.new(wallets: home.wallets, log: test_log).run(%W[set #{wallet.id} my-alias])
Zold::Alias.new(wallets: home.wallets, log: fake_log).run(%W[set #{wallet.id} my-alias])
assert_equal read_alias_file(home), %W[my-alias #{wallet.id}]
end
end
Expand All @@ -23,9 +23,9 @@ def test_set_writes_alias_to_the_alias_file
# The syntax is already documented in the alias command in the help.
def test_remove_removes_the_alias_from_the_alias_file
skip
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
cmd = Zold::Alias.new(wallets: home.wallets, log: test_log)
cmd = Zold::Alias.new(wallets: home.wallets, log: fake_log)
cmd.run(%W[set #{wallet.id} my-alias])
assert_equal read_alias_file(home), %W[my-alias #{wallet.id}]
cmd.run(%w[remove my-alias])
Expand All @@ -38,9 +38,9 @@ def test_remove_removes_the_alias_from_the_alias_file
# The syntax is already documented in the alias command in the help.
def test_show_prints_out_the_aliased_wallet_id
skip
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
cmd = Zold::Alias.new(wallets: home.wallets, log: test_log)
cmd = Zold::Alias.new(wallets: home.wallets, log: fake_log)
cmd.run(%W[set #{wallet.id} my-alias])
assert_equal read_alias_file(home), %W[my-alias #{wallet.id}]
stdout, = capture_io { cmd.run(%w[show my-alias]) }
Expand Down
2 changes: 1 addition & 1 deletion test/commands/test_calculate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
# License:: MIT
class TestCalculate < Zold::Test
def test_calculates_score
score = Zold::Calculate.new(log: test_log).run(
score = Zold::Calculate.new(log: fake_log).run(
['score', '--strength=2', '--max=8', '--invoice=NOSUFFIX@ffffffffffffffff']
)
assert(score.valid?)
Expand Down
16 changes: 8 additions & 8 deletions test/commands/test_clean.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,38 +34,38 @@
# License:: MIT
class TestClean < Zold::Test
def test_cleans_copies
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
copies = home.copies(wallet)
copies.add('a1', 'host-1', 80, 1, time: Time.now - (26 * 60 * 60))
copies.add('a2', 'host-2', 80, 2, time: Time.now - (26 * 60 * 60))
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: test_log).run(['clean', wallet.id.to_s])
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: fake_log).run(['clean', wallet.id.to_s])
assert(copies.all.empty?)
end
end

def test_clean_no_copies
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
copies = home.copies(wallet)
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: test_log).run(['clean'])
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: fake_log).run(['clean'])
assert(copies.all.empty?)
end
end

def test_cleans_empty_wallets
FakeHome.new(log: test_log).run do |home|
Zold::Clean.new(wallets: home.wallets, copies: File.join(home.dir, 'c'), log: test_log).run(['clean'])
FakeHome.new(log: fake_log).run do |home|
Zold::Clean.new(wallets: home.wallets, copies: File.join(home.dir, 'c'), log: fake_log).run(['clean'])
end
end

def test_cleans_copies_in_threads
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
copies = home.copies(wallet)
copies.add(File.read(wallet.path), 'host-2', 80, 2, time: Time.now)
Threads.new(20).assert do
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: test_log).run(['clean'])
Zold::Clean.new(wallets: home.wallets, copies: copies.root, log: fake_log).run(['clean'])
end
assert_equal(1, copies.all.count)
end
Expand Down
2 changes: 1 addition & 1 deletion test/commands/test_create.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class TestCreate < Zold::Test
def test_creates_wallet
Dir.mktmpdir do |dir|
wallets = Zold::Wallets.new(dir)
id = Zold::Create.new(wallets: wallets, remotes: nil, log: test_log).run(
id = Zold::Create.new(wallets: wallets, remotes: nil, log: fake_log).run(
['create', '--public-key=fixtures/id_rsa.pub', '--skip-test']
)
wallets.acq(id) do |wallet|
Expand Down
6 changes: 3 additions & 3 deletions test/commands/test_diff.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,19 @@
# License:: MIT
class TestDiff < Zold::Test
def test_diff_with_copies
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
first = home.create_wallet
File.write(first.path, File.read(wallet.path))
second = home.create_wallet
File.write(second.path, File.read(wallet.path))
Zold::Pay.new(wallets: home.wallets, copies: home.dir, remotes: home.remotes, log: test_log).run(
Zold::Pay.new(wallets: home.wallets, copies: home.dir, remotes: home.remotes, log: fake_log).run(
['pay', wallet.id.to_s, "NOPREFIX@#{Zold::Id.new}", '14.95', '--force', '--private-key=fixtures/id_rsa']
)
copies = home.copies(wallet)
copies.add(File.read(first.path), 'host-1', 80, 5)
copies.add(File.read(second.path), 'host-2', 80, 5)
diff = Zold::Diff.new(wallets: home.wallets, copies: copies.root, log: test_log).run(
diff = Zold::Diff.new(wallets: home.wallets, copies: copies.root, log: fake_log).run(
['diff', wallet.id.to_s]
)
assert(diff.include?('-0001;'), diff)
Expand Down
14 changes: 7 additions & 7 deletions test/commands/test_fetch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
# License:: MIT
class TestFetch < Zold::Test
def test_fetches_wallet
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
status: 200,
Expand All @@ -59,7 +59,7 @@ def test_fetches_wallet
remotes.add('localhost', 4096)
remotes.add('localhost', 81)
copies = home.copies(wallet)
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: test_log).run(
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: fake_log).run(
['fetch', '--tolerate-edges', '--tolerate-quorum=1', '--ignore-score-weakness', wallet.id.to_s]
)
assert_equal(1, copies.all.count)
Expand All @@ -69,7 +69,7 @@ def test_fetches_wallet
end

def test_fetches_multiple_wallets
log = TestLogger.new(test_log)
log = TestLogger.new(fake_log)
FakeHome.new(log: log).run do |home|
wallet_a = home.create_wallet
stub_request(:get, "http://localhost:4096/wallet/#{wallet_a.id}").to_return(
Expand Down Expand Up @@ -113,7 +113,7 @@ def test_fetches_multiple_wallets
end

def test_fails_when_only_edge_nodes
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
status: 200,
Expand All @@ -129,15 +129,15 @@ def test_fails_when_only_edge_nodes
remotes.add('localhost', 4096)
copies = home.copies(wallet)
assert_raises Zold::Fetch::EdgesOnly do
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: test_log).run(
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: fake_log).run(
['fetch', '--ignore-score-weakness', wallet.id.to_s]
)
end
end
end

def test_fails_when_only_one_node
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
stub_request(:get, "http://localhost:4096/wallet/#{wallet.id}").to_return(
status: 200,
Expand All @@ -153,7 +153,7 @@ def test_fails_when_only_one_node
remotes.add('localhost', 4096)
copies = home.copies(wallet)
assert_raises Zold::Fetch::NoQuorum do
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: test_log).run(
Zold::Fetch.new(wallets: home.wallets, copies: copies.root, remotes: remotes, log: fake_log).run(
['fetch', '--tolerate-edges', '--ignore-score-weakness', wallet.id.to_s]
)
end
Expand Down
2 changes: 1 addition & 1 deletion test/commands/test_invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_generates_invoice
wallets = Zold::Wallets.new(dir)
wallets.acq(id) do |source|
source.init(id, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
invoice = Zold::Invoice.new(wallets: wallets, remotes: nil, copies: nil, log: test_log).run(
invoice = Zold::Invoice.new(wallets: wallets, remotes: nil, copies: nil, log: fake_log).run(
['invoice', id.to_s, '--length=16']
)
assert_equal(33, invoice.length)
Expand Down
2 changes: 1 addition & 1 deletion test/commands/test_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_lists_wallets_with_balances
wallets = Zold::Wallets.new(dir)
wallets.acq(id) do |wallet|
wallet.init(Zold::Id.new, Zold::Key.new(file: 'fixtures/id_rsa.pub'))
Zold::List.new(wallets: wallets, copies: File.join(dir, 'copies'), log: test_log).run
Zold::List.new(wallets: wallets, copies: File.join(dir, 'copies'), log: fake_log).run
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions test/commands/test_merge.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@
# License:: MIT
class TestMerge < Zold::Test
def test_merges_wallet
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
first = home.create_wallet
File.write(first.path, File.read(wallet.path))
second = home.create_wallet
File.write(second.path, File.read(wallet.path))
Zold::Pay.new(wallets: home.wallets, copies: home.dir, remotes: home.remotes, log: test_log).run(
Zold::Pay.new(wallets: home.wallets, copies: home.dir, remotes: home.remotes, log: fake_log).run(
['pay', wallet.id.to_s, "NOPREFIX@#{Zold::Id.new}", '14.95', '--force', '--private-key=fixtures/id_rsa']
)
copies = home.copies(wallet)
copies.add(File.read(first.path), 'host-1', 80, 5)
copies.add(File.read(second.path), 'host-2', 80, 5)
modified = Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: test_log).run(
modified = Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: fake_log).run(
['merge', wallet.id.to_s]
)
assert(1, modified.count)
Expand All @@ -64,41 +64,41 @@ def test_merges_wallet
end

def test_merges_with_a_broken_copy
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet
copies = home.copies(wallet)
copies.add(File.read(wallet.path), 'good-host', 80, 5)
copies.add('some garbage', 'bad-host', 80, 5)
modified = Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: test_log).run(
modified = Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: fake_log).run(
['merge', wallet.id.to_s]
)
assert(modified.empty?)
end
end

def test_merges_a_copy_on_top
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
wallet = home.create_wallet(Zold::Id::ROOT)
copies = home.copies(wallet)
copies.add(File.read(wallet.path), 'good-host', 80, 5)
key = Zold::Key.new(file: 'fixtures/id_rsa')
wallet.sub(Zold::Amount.new(zld: 9.99), "NOPREFIX@#{Zold::Id.new}", key)
Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: test_log).run(
Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: fake_log).run(
['merge', wallet.id.to_s]
)
assert(!wallet.balance.zero?)
end
end

def test_rejects_fake_positives_in_new_wallet
FakeHome.new(log: test_log).run do |home|
FakeHome.new(log: fake_log).run do |home|
main = home.create_wallet
remote = home.create_wallet
File.write(remote.path, File.read(main.path))
remote.add(Zold::Txn.new(1, Time.now, Zold::Amount.new(zld: 11.0), 'NOPREFIX', Zold::Id.new, 'fake'))
copies = home.copies(main)
copies.add(File.read(remote.path), 'fake-host', 80, 0)
Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: test_log).run(
Zold::Merge.new(wallets: home.wallets, remotes: home.remotes, copies: copies.root, log: fake_log).run(
['merge', main.id.to_s, '--no-baseline']
)
assert_equal(Zold::Amount::ZERO, main.balance)
Expand All @@ -117,7 +117,7 @@ def test_rejects_fake_positives_in_new_wallet
wallets = Zold::Wallets.new(dir)
copies = File.join(dir, 'copies')
remotes = Zold::Remotes.new(file: File.join(dir, 'remotes'))
Zold::Merge.new(wallets: wallets, remotes: remotes, copies: copies, log: test_log).run(
Zold::Merge.new(wallets: wallets, remotes: remotes, copies: copies, log: fake_log).run(
%w[merge 0123456789abcdef] + File.read(File.join(dir, 'opts')).split("\n")
)
Dir.chdir(dir) do
Expand Down
Loading

0 comments on commit 1f2cebc

Please sign in to comment.