Skip to content

Commit 69f7fc2

Browse files
committed
🚚 Move IMAP#responses tests to their own file
`test_imap.rb` is huge! `wait_for_response_count` was moved into TestCase (in test/lib/helper). Perhaps this should be moved into a shared module or an IMAPTestCase class (subclass of TestCase) instead?
1 parent 0c44378 commit 69f7fc2

File tree

3 files changed

+139
-112
lines changed

3 files changed

+139
-112
lines changed

test/lib/helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,16 @@
22
require "core_assertions"
33

44
Test::Unit::TestCase.include Test::Unit::CoreAssertions
5+
6+
class Test::Unit::TestCase
7+
def wait_for_response_count(imap, type:, count:,
8+
timeout: 0.5, interval: 0.001)
9+
deadline = Time.now + timeout
10+
loop do
11+
current_count = imap.responses(type, &:size)
12+
break :count if count <= current_count
13+
break :deadline if deadline < Time.now
14+
sleep interval
15+
end
16+
end
17+
end

test/net/imap/test_imap.rb

Lines changed: 0 additions & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,107 +1110,6 @@ def test_enable
11101110
end
11111111
end
11121112

1113-
test "#responses" do
1114-
with_fake_server do |server, imap|
1115-
# responses available before SELECT/EXAMINE
1116-
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
1117-
imap.responses("CAPABILITY", &:last))
1118-
resp = imap.select "INBOX"
1119-
# responses are cleared after SELECT/EXAMINE
1120-
assert_equal(nil, imap.responses("CAPABILITY", &:last))
1121-
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
1122-
[resp.class, resp.tag, resp.name])
1123-
assert_equal([172], imap.responses { _1["EXISTS"] })
1124-
assert_equal([3857529045], imap.responses("UIDVALIDITY") { _1 })
1125-
assert_equal(1, imap.responses("RECENT", &:last))
1126-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1127-
# Deprecated style, without a block:
1128-
imap.config.responses_without_block = :raise
1129-
assert_raise(ArgumentError) do imap.responses end
1130-
imap.config.responses_without_block = :warn
1131-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1132-
assert_warn(/Pass a block.*or.*clear_responses/i) do
1133-
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1134-
imap.responses["FLAGS"]&.last)
1135-
end
1136-
# TODO: assert_no_warn?
1137-
imap.config.responses_without_block = :silence_deprecation_warning
1138-
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
1139-
stderr = EnvUtil.verbose_warning {
1140-
assert_equal(%i[Answered Flagged Deleted Seen Draft],
1141-
imap.responses["FLAGS"]&.last)
1142-
}
1143-
assert_empty stderr
1144-
end
1145-
end
1146-
1147-
test "#clear_responses" do
1148-
with_fake_server do |server, imap|
1149-
resp = imap.select "INBOX"
1150-
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
1151-
[resp.class, resp.tag, resp.name])
1152-
# called with "type", clears and returns only that type
1153-
assert_equal([172], imap.clear_responses("EXISTS"))
1154-
assert_equal([], imap.clear_responses("EXISTS"))
1155-
assert_equal([1], imap.clear_responses("RECENT"))
1156-
assert_equal([3857529045], imap.clear_responses("UIDVALIDITY"))
1157-
# called without "type", clears and returns all responses
1158-
responses = imap.clear_responses
1159-
assert_equal([], responses["EXISTS"])
1160-
assert_equal([], responses["RECENT"])
1161-
assert_equal([], responses["UIDVALIDITY"])
1162-
assert_equal([12], responses["UNSEEN"])
1163-
assert_equal([4392], responses["UIDNEXT"])
1164-
assert_equal(5, responses["FLAGS"].last&.size)
1165-
assert_equal(3, responses["PERMANENTFLAGS"].last&.size)
1166-
assert_equal({}, imap.responses(&:itself))
1167-
assert_equal({}, imap.clear_responses)
1168-
end
1169-
end
1170-
1171-
test "#extract_responses" do
1172-
with_fake_server do |server, imap|
1173-
resp = imap.select "INBOX"
1174-
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
1175-
[resp.class, resp.tag, resp.name])
1176-
# Need to send a string type and a block
1177-
assert_raise(ArgumentError) do imap.extract_responses { true } end
1178-
assert_raise(ArgumentError) do imap.extract_responses(nil) { true } end
1179-
assert_raise(ArgumentError) do imap.extract_responses("OK") end
1180-
# matching nothing
1181-
assert_equal([172], imap.responses("EXISTS", &:dup))
1182-
assert_equal([], imap.extract_responses("EXISTS") { String === _1 })
1183-
assert_equal([172], imap.responses("EXISTS", &:dup))
1184-
# matching everything
1185-
assert_equal([172], imap.responses("EXISTS", &:dup))
1186-
assert_equal([172], imap.extract_responses("EXISTS", &:even?))
1187-
assert_equal([], imap.responses("EXISTS", &:dup))
1188-
# matching some
1189-
server.unsolicited("101 FETCH (UID 1111 FLAGS (\\Seen))")
1190-
server.unsolicited("102 FETCH (UID 2222 FLAGS (\\Seen \\Flagged))")
1191-
server.unsolicited("103 FETCH (UID 3333 FLAGS (\\Deleted))")
1192-
wait_for_response_count(imap, type: "FETCH", count: 3)
1193-
1194-
result = imap.extract_responses("FETCH") { _1.flags.include?(:Flagged) }
1195-
assert_equal(
1196-
[
1197-
Net::IMAP::FetchData.new(
1198-
102, {"UID" => 2222, "FLAGS" => [:Seen, :Flagged]}
1199-
),
1200-
],
1201-
result,
1202-
)
1203-
assert_equal 2, imap.responses("FETCH", &:count)
1204-
1205-
result = imap.extract_responses("FETCH") { _1.flags.include?(:Deleted) }
1206-
assert_equal(
1207-
[Net::IMAP::FetchData.new(103, {"UID" => 3333, "FLAGS" => [:Deleted]})],
1208-
result
1209-
)
1210-
assert_equal 1, imap.responses("FETCH", &:count)
1211-
end
1212-
end
1213-
12141113
test "#select with condstore" do
12151114
with_fake_server do |server, imap|
12161115
imap.select "inbox", condstore: true
@@ -1467,15 +1366,4 @@ def server_addr
14671366
Addrinfo.tcp("localhost", 0).ip_address
14681367
end
14691368

1470-
def wait_for_response_count(imap, type:, count:,
1471-
timeout: 0.5, interval: 0.001)
1472-
deadline = Time.now + timeout
1473-
loop do
1474-
current_count = imap.responses(type, &:size)
1475-
break :count if count <= current_count
1476-
break :deadline if deadline < Time.now
1477-
sleep interval
1478-
end
1479-
end
1480-
14811369
end

test/net/imap/test_imap_responses.rb

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
# frozen_string_literal: true
2+
3+
require "net/imap"
4+
require "test/unit"
5+
require_relative "fake_server"
6+
7+
class IMAPResponsesTest < Test::Unit::TestCase
8+
include Net::IMAP::FakeServer::TestHelper
9+
10+
def setup
11+
Net::IMAP.config.reset
12+
@do_not_reverse_lookup = Socket.do_not_reverse_lookup
13+
Socket.do_not_reverse_lookup = true
14+
@threads = []
15+
end
16+
17+
def teardown
18+
if !@threads.empty?
19+
assert_join_threads(@threads)
20+
end
21+
ensure
22+
Socket.do_not_reverse_lookup = @do_not_reverse_lookup
23+
end
24+
25+
test "#responses" do
26+
with_fake_server do |server, imap|
27+
# responses available before SELECT/EXAMINE
28+
assert_equal(%w[IMAP4REV1 NAMESPACE MOVE IDLE UTF8=ACCEPT],
29+
imap.responses("CAPABILITY", &:last))
30+
resp = imap.select "INBOX"
31+
# responses are cleared after SELECT/EXAMINE
32+
assert_equal(nil, imap.responses("CAPABILITY", &:last))
33+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
34+
[resp.class, resp.tag, resp.name])
35+
assert_equal([172], imap.responses { _1["EXISTS"] })
36+
assert_equal([3857529045], imap.responses("UIDVALIDITY") { _1 })
37+
assert_equal(1, imap.responses("RECENT", &:last))
38+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
39+
# Deprecated style, without a block:
40+
imap.config.responses_without_block = :raise
41+
assert_raise(ArgumentError) do imap.responses end
42+
imap.config.responses_without_block = :warn
43+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
44+
assert_warn(/Pass a block.*or.*clear_responses/i) do
45+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
46+
imap.responses["FLAGS"]&.last)
47+
end
48+
# TODO: assert_no_warn?
49+
imap.config.responses_without_block = :silence_deprecation_warning
50+
assert_raise(ArgumentError) do imap.responses("UIDNEXT") end
51+
stderr = EnvUtil.verbose_warning {
52+
assert_equal(%i[Answered Flagged Deleted Seen Draft],
53+
imap.responses["FLAGS"]&.last)
54+
}
55+
assert_empty stderr
56+
end
57+
end
58+
59+
test "#clear_responses" do
60+
with_fake_server do |server, imap|
61+
resp = imap.select "INBOX"
62+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
63+
[resp.class, resp.tag, resp.name])
64+
# called with "type", clears and returns only that type
65+
assert_equal([172], imap.clear_responses("EXISTS"))
66+
assert_equal([], imap.clear_responses("EXISTS"))
67+
assert_equal([1], imap.clear_responses("RECENT"))
68+
assert_equal([3857529045], imap.clear_responses("UIDVALIDITY"))
69+
# called without "type", clears and returns all responses
70+
responses = imap.clear_responses
71+
assert_equal([], responses["EXISTS"])
72+
assert_equal([], responses["RECENT"])
73+
assert_equal([], responses["UIDVALIDITY"])
74+
assert_equal([12], responses["UNSEEN"])
75+
assert_equal([4392], responses["UIDNEXT"])
76+
assert_equal(5, responses["FLAGS"].last&.size)
77+
assert_equal(3, responses["PERMANENTFLAGS"].last&.size)
78+
assert_equal({}, imap.responses(&:itself))
79+
assert_equal({}, imap.clear_responses)
80+
end
81+
end
82+
83+
test "#extract_responses" do
84+
with_fake_server do |server, imap|
85+
resp = imap.select "INBOX"
86+
assert_equal([Net::IMAP::TaggedResponse, "RUBY0001", "OK"],
87+
[resp.class, resp.tag, resp.name])
88+
# Need to send a string type and a block
89+
assert_raise(ArgumentError) do imap.extract_responses { true } end
90+
assert_raise(ArgumentError) do imap.extract_responses(nil) { true } end
91+
assert_raise(ArgumentError) do imap.extract_responses("OK") end
92+
# matching nothing
93+
assert_equal([172], imap.responses("EXISTS", &:dup))
94+
assert_equal([], imap.extract_responses("EXISTS") { String === _1 })
95+
assert_equal([172], imap.responses("EXISTS", &:dup))
96+
# matching everything
97+
assert_equal([172], imap.responses("EXISTS", &:dup))
98+
assert_equal([172], imap.extract_responses("EXISTS", &:even?))
99+
assert_equal([], imap.responses("EXISTS", &:dup))
100+
# matching some
101+
server.unsolicited("101 FETCH (UID 1111 FLAGS (\\Seen))")
102+
server.unsolicited("102 FETCH (UID 2222 FLAGS (\\Seen \\Flagged))")
103+
server.unsolicited("103 FETCH (UID 3333 FLAGS (\\Deleted))")
104+
wait_for_response_count(imap, type: "FETCH", count: 3)
105+
106+
result = imap.extract_responses("FETCH") { _1.flags.include?(:Flagged) }
107+
assert_equal(
108+
[
109+
Net::IMAP::FetchData.new(
110+
102, {"UID" => 2222, "FLAGS" => [:Seen, :Flagged]}
111+
),
112+
],
113+
result,
114+
)
115+
assert_equal 2, imap.responses("FETCH", &:count)
116+
117+
result = imap.extract_responses("FETCH") { _1.flags.include?(:Deleted) }
118+
assert_equal(
119+
[Net::IMAP::FetchData.new(103, {"UID" => 3333, "FLAGS" => [:Deleted]})],
120+
result
121+
)
122+
assert_equal 1, imap.responses("FETCH", &:count)
123+
end
124+
end
125+
126+
end

0 commit comments

Comments
 (0)