imap-backup 4.0.5 → 4.1.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. checksums.yaml +4 -4
  2. data/bin/imap-backup +5 -2
  3. data/lib/imap/backup/account/connection.rb +70 -58
  4. data/lib/imap/backup/account/folder.rb +23 -3
  5. data/lib/imap/backup/account.rb +6 -7
  6. data/lib/imap/backup/cli/accounts.rb +43 -0
  7. data/lib/imap/backup/cli/folders.rb +3 -1
  8. data/lib/imap/backup/cli/helpers.rb +8 -9
  9. data/lib/imap/backup/cli/local.rb +4 -2
  10. data/lib/imap/backup/cli/setup.rb +1 -1
  11. data/lib/imap/backup/cli/status.rb +1 -1
  12. data/lib/imap/backup/cli/utils.rb +3 -2
  13. data/lib/imap/backup/{configuration/store.rb → configuration.rb} +49 -14
  14. data/lib/imap/backup/downloader.rb +26 -12
  15. data/lib/imap/backup/logger.rb +42 -0
  16. data/lib/imap/backup/sanitizer.rb +42 -0
  17. data/lib/imap/backup/serializer/mbox_store.rb +2 -2
  18. data/lib/imap/backup/{configuration → setup}/account.rb +59 -41
  19. data/lib/imap/backup/{configuration → setup}/asker.rb +5 -5
  20. data/lib/imap/backup/setup/connection_tester.rb +26 -0
  21. data/lib/imap/backup/{configuration → setup}/folder_chooser.rb +25 -17
  22. data/lib/imap/backup/setup/helpers.rb +15 -0
  23. data/lib/imap/backup/{configuration/setup.rb → setup.rb} +33 -25
  24. data/lib/imap/backup/uploader.rb +2 -2
  25. data/lib/imap/backup/version.rb +2 -2
  26. data/lib/imap/backup.rb +7 -33
  27. data/lib/retry_on_error.rb +1 -1
  28. data/spec/features/backup_spec.rb +1 -0
  29. data/spec/features/status_spec.rb +43 -0
  30. data/spec/features/support/email_server.rb +5 -2
  31. data/spec/features/support/shared/connection_context.rb +7 -5
  32. data/spec/support/higline_test_helpers.rb +1 -1
  33. data/spec/support/silence_logging.rb +1 -1
  34. data/spec/unit/email/provider/base_spec.rb +1 -1
  35. data/spec/unit/email/provider_spec.rb +2 -2
  36. data/spec/unit/imap/backup/account/connection_spec.rb +22 -26
  37. data/spec/unit/imap/backup/cli/accounts_spec.rb +47 -0
  38. data/spec/unit/imap/backup/cli/local_spec.rb +15 -4
  39. data/spec/unit/imap/backup/cli/utils_spec.rb +54 -42
  40. data/spec/unit/imap/backup/{configuration/store_spec.rb → configuration_spec.rb} +23 -24
  41. data/spec/unit/imap/backup/downloader_spec.rb +1 -1
  42. data/spec/unit/imap/backup/logger_spec.rb +48 -0
  43. data/spec/unit/imap/backup/{configuration → setup}/account_spec.rb +78 -70
  44. data/spec/unit/imap/backup/{configuration → setup}/asker_spec.rb +2 -2
  45. data/spec/unit/imap/backup/{configuration → setup}/connection_tester_spec.rb +10 -10
  46. data/spec/unit/imap/backup/{configuration → setup}/folder_chooser_spec.rb +25 -26
  47. data/spec/unit/imap/backup/{configuration/setup_spec.rb → setup_spec.rb} +81 -52
  48. metadata +51 -48
  49. data/lib/imap/backup/configuration/connection_tester.rb +0 -14
  50. data/lib/imap/backup/configuration/list.rb +0 -53
  51. data/spec/support/shared_examples/account_flagging.rb +0 -23
  52. data/spec/unit/imap/backup/configuration/list_spec.rb +0 -89
  53. data/spec/unit/imap/backup_spec.rb +0 -28
@@ -5,7 +5,7 @@ module RetryOnError
5
5
  rescue *errors => e
6
6
  if tries < limit
7
7
  message = "#{e}, attempt #{tries} of #{limit}"
8
- Imap::Backup.logger.debug message
8
+ Imap::Backup::Logger.logger.debug message
9
9
  tries += 1
10
10
  retry
11
11
  end
@@ -58,6 +58,7 @@ RSpec.describe "backup", type: :feature, docker: true do
58
58
  email3
59
59
  original_folder_uid_validity
60
60
  connection.run_backup
61
+ connection.disconnect
61
62
  server_rename_folder folder, new_name
62
63
  end
63
64
  let(:renamed_folder) { "#{folder}.#{original_folder_uid_validity}" }
@@ -0,0 +1,43 @@
1
+ require "features/helper"
2
+ require "imap/backup/cli/status"
3
+
4
+ RSpec.describe "status", type: :feature, docker: true do
5
+ include_context "imap-backup connection"
6
+ include_context "message-fixtures"
7
+
8
+ context "when there are non-backed-up messages" do
9
+ let(:options) do
10
+ {accounts: "address@example.org"}
11
+ end
12
+ let(:folder) { "my-stuff" }
13
+ let(:backup_folders) { [{name: folder}] }
14
+ let(:email1) { send_email folder, msg1 }
15
+ let(:output) { StringIO.new }
16
+
17
+ before do
18
+ allow(Imap::Backup::CLI::Accounts).to receive(:new) { [account] }
19
+ server_create_folder folder
20
+ email1
21
+ end
22
+
23
+ around do |example|
24
+ stdout = $stdout
25
+ $stdout = output
26
+ example.run
27
+ $stdout = stdout
28
+ end
29
+
30
+ after do
31
+ FileUtils.rm_rf local_backup_path
32
+ delete_emails folder
33
+ server_delete_folder folder
34
+ connection.disconnect
35
+ end
36
+
37
+ it "prints the number" do
38
+ Imap::Backup::CLI::Status.new(options).run
39
+
40
+ expect(output.string).to eq("address@example.org\nmy-stuff: 1\n")
41
+ end
42
+ end
43
+ end
@@ -63,11 +63,14 @@ module EmailServerHelpers
63
63
  end
64
64
 
65
65
  def server_folders
66
- root_info = imap.list("", "")[0]
67
- root = root_info.name
68
66
  imap.list(root, "*")
69
67
  end
70
68
 
69
+ def root
70
+ root_info = imap.list("", "")[0]
71
+ root_info.name
72
+ end
73
+
71
74
  def server_create_folder(folder)
72
75
  imap.create(folder)
73
76
  imap.disconnect
@@ -2,11 +2,13 @@ shared_context "imap-backup connection" do
2
2
  let(:local_backup_path) { Dir.mktmpdir(nil, "tmp") }
3
3
  let(:default_connection) { fixture("connection") }
4
4
  let(:backup_folders) { nil }
5
- let(:connection_options) do
6
- default_connection.merge(
7
- local_path: local_backup_path,
8
- folders: backup_folders
5
+ let(:account) do
6
+ Imap::Backup::Account.new(
7
+ default_connection.merge(
8
+ local_path: local_backup_path,
9
+ folders: backup_folders
10
+ )
9
11
  )
10
12
  end
11
- let(:connection) { Imap::Backup::Account::Connection.new(connection_options) }
13
+ let(:connection) { Imap::Backup::Account::Connection.new(account) }
12
14
  end
@@ -2,7 +2,7 @@ module HighLineTestHelpers
2
2
  def prepare_highline
3
3
  @input = instance_double(IO, eof?: false, gets: "q\n")
4
4
  @output = StringIO.new
5
- Imap::Backup::Configuration::Setup.highline = HighLine.new(@input, @output)
5
+ Imap::Backup::Setup.highline = HighLine.new(@input, @output)
6
6
  [@input, @output]
7
7
  end
8
8
  end
@@ -1,7 +1,7 @@
1
1
  def silence_logging
2
2
  RSpec.configure do |config|
3
3
  config.before(:suite) do
4
- Imap::Backup.logger.level = Logger::UNKNOWN
4
+ Imap::Backup::Logger.logger.level = Logger::UNKNOWN
5
5
  end
6
6
  end
7
7
  end
@@ -1,4 +1,4 @@
1
- describe Email::Provider::Default do
1
+ describe Email::Provider::Base do
2
2
  describe "#options" do
3
3
  it "returns options" do
4
4
  expect(subject.options).to be_a(Hash)
@@ -17,10 +17,10 @@ describe Email::Provider do
17
17
  end
18
18
 
19
19
  context "with unknown providers" do
20
- it "returns a default provider" do
20
+ it "returns the Unknown provider" do
21
21
  result = described_class.for_address("foo@unknown.com")
22
22
 
23
- expect(result).to be_a(Email::Provider::Default)
23
+ expect(result).to be_a(Email::Provider::Unknown)
24
24
  end
25
25
  end
26
26
  end
@@ -5,6 +5,7 @@ describe Imap::Backup::Account::Connection do
5
5
  FOLDER_CONFIG = {name: BACKUP_FOLDER}.freeze
6
6
  FOLDER_NAME = "my_folder".freeze
7
7
  GMAIL_IMAP_SERVER = "imap.gmail.com".freeze
8
+ IMAP_FOLDER = "imap_folder".freeze
8
9
  LOCAL_PATH = "local_path".freeze
9
10
  LOCAL_UID = "local_uid".freeze
10
11
  PASSWORD = "secret".freeze
@@ -12,7 +13,7 @@ describe Imap::Backup::Account::Connection do
12
13
  SERVER = "imap.example.com".freeze
13
14
  USERNAME = "username@example.com".freeze
14
15
 
15
- subject { described_class.new(options) }
16
+ subject { described_class.new(account) }
16
17
 
17
18
  let(:client) do
18
19
  instance_double(
@@ -20,14 +21,16 @@ describe Imap::Backup::Account::Connection do
20
21
  )
21
22
  end
22
23
  let(:imap_folders) { [] }
23
- let(:options) do
24
- {
24
+ let(:account) do
25
+ instance_double(
26
+ Imap::Backup::Account,
25
27
  username: USERNAME,
26
28
  password: PASSWORD,
27
29
  local_path: LOCAL_PATH,
28
30
  folders: config_folders,
29
- server: server
30
- }
31
+ server: server,
32
+ connection_options: nil
33
+ )
31
34
  end
32
35
  let(:config_folders) { [FOLDER_CONFIG] }
33
36
  let(:root_info) do
@@ -59,21 +62,10 @@ describe Imap::Backup::Account::Connection do
59
62
  end
60
63
 
61
64
  describe "#initialize" do
62
- [
63
- [:username, USERNAME],
64
- [:password, PASSWORD],
65
- [:local_path, LOCAL_PATH],
66
- [:server, SERVER]
67
- ].each do |attr, expected|
68
- it "expects #{attr}" do
69
- expect(subject.public_send(attr)).to eq(expected)
70
- end
71
- end
72
-
73
65
  it "creates the path" do
74
66
  expect(Imap::Backup::Utils).to receive(:make_folder)
75
67
 
76
- subject.username
68
+ subject
77
69
  end
78
70
  end
79
71
 
@@ -110,19 +102,23 @@ describe Imap::Backup::Account::Connection do
110
102
  end
111
103
  end
112
104
 
113
- describe "#folders" do
105
+ describe "#folder_names" do
114
106
  let(:imap_folders) do
115
- [BACKUP_FOLDER]
107
+ [IMAP_FOLDER]
116
108
  end
117
109
 
118
110
  it "returns the list of folders" do
119
- expect(subject.folders).to eq([BACKUP_FOLDER])
111
+ expect(subject.folder_names).to eq([IMAP_FOLDER])
120
112
  end
121
113
  end
122
114
 
123
115
  describe "#status" do
124
116
  let(:folder) do
125
- instance_double(Imap::Backup::Account::Folder, uids: [remote_uid])
117
+ instance_double(
118
+ Imap::Backup::Account::Folder,
119
+ uids: [remote_uid],
120
+ name: IMAP_FOLDER
121
+ )
126
122
  end
127
123
  let(:remote_uid) { "remote_uid" }
128
124
 
@@ -132,7 +128,7 @@ describe Imap::Backup::Account::Connection do
132
128
  end
133
129
 
134
130
  it "returns the names of folders" do
135
- expect(subject.status[0][:name]).to eq(BACKUP_FOLDER)
131
+ expect(subject.status[0][:name]).to eq(IMAP_FOLDER)
136
132
  end
137
133
 
138
134
  it "returns local message uids" do
@@ -148,7 +144,7 @@ describe Imap::Backup::Account::Connection do
148
144
  let(:folder) do
149
145
  instance_double(
150
146
  Imap::Backup::Account::Folder,
151
- name: "folder",
147
+ name: IMAP_FOLDER,
152
148
  exist?: exists,
153
149
  uid_validity: uid_validity
154
150
  )
@@ -159,11 +155,11 @@ describe Imap::Backup::Account::Connection do
159
155
 
160
156
  before do
161
157
  allow(Imap::Backup::Downloader).
162
- to receive(:new).with(folder, serializer) { downloader }
158
+ to receive(:new).with(folder, serializer, anything) { downloader }
163
159
  allow(Imap::Backup::Account::Folder).to receive(:new).
164
160
  with(subject, BACKUP_FOLDER) { folder }
165
161
  allow(Imap::Backup::Serializer::Mbox).to receive(:new).
166
- with(LOCAL_PATH, BACKUP_FOLDER) { serializer }
162
+ with(LOCAL_PATH, IMAP_FOLDER) { serializer }
167
163
  end
168
164
 
169
165
  context "with supplied config_folders" do
@@ -257,7 +253,7 @@ describe Imap::Backup::Account::Connection do
257
253
  Imap::Backup::Account::Folder,
258
254
  create: nil,
259
255
  uids: uids,
260
- name: FOLDER_NAME,
256
+ name: IMAP_FOLDER,
261
257
  uid_validity: uid_validity
262
258
  )
263
259
  end
@@ -0,0 +1,47 @@
1
+ require "imap/backup/cli/accounts"
2
+
3
+ describe Imap::Backup::CLI::Accounts do
4
+ subject { described_class.new }
5
+
6
+ let(:accounts) { [account1, account2] }
7
+ let(:account1) do
8
+ instance_double(
9
+ Imap::Backup::Account,
10
+ username: "a1@example.com"
11
+ )
12
+ end
13
+ let(:account2) do
14
+ instance_double(
15
+ Imap::Backup::Account,
16
+ username: "a2@example.com"
17
+ )
18
+ end
19
+ let(:store) do
20
+ instance_double(Imap::Backup::Configuration, accounts: accounts)
21
+ end
22
+ let(:exists) { true }
23
+
24
+ before do
25
+ allow(Imap::Backup::Configuration).to receive(:new) { store }
26
+ allow(Imap::Backup::Configuration).
27
+ to receive(:exist?) { exists }
28
+ end
29
+
30
+ describe "#each" do
31
+ specify "calls the block with each account" do
32
+ result = subject.map { |a| a }
33
+
34
+ expect(result).to eq(accounts)
35
+ end
36
+
37
+ context "when the configuration file is missing" do
38
+ let(:exists) { false }
39
+
40
+ it "fails" do
41
+ expect do
42
+ subject.each {}
43
+ end.to raise_error(Imap::Backup::ConfigurationNotFound, /not found/)
44
+ end
45
+ end
46
+ end
47
+ end
@@ -1,8 +1,18 @@
1
1
  describe Imap::Backup::CLI::Local do
2
- let(:list) do
3
- instance_double(Imap::Backup::Configuration::List, accounts: accounts)
2
+ let(:accounts) do
3
+ instance_double(
4
+ Imap::Backup::CLI::Accounts,
5
+ find: ->(&block) { [account].find { |a| block.call(a) } }
6
+ )
7
+ end
8
+ let(:account) do
9
+ instance_double(
10
+ Imap::Backup::Account,
11
+ username: email,
12
+ marked_for_deletion?: false,
13
+ modified?: false
14
+ )
4
15
  end
5
- let(:accounts) { [{username: email}] }
6
16
  let(:connection) do
7
17
  instance_double(
8
18
  Imap::Backup::Account::Connection,
@@ -31,9 +41,10 @@ describe Imap::Backup::CLI::Local do
31
41
 
32
42
  before do
33
43
  allow(Kernel).to receive(:puts)
34
- allow(Imap::Backup::Configuration::List).to receive(:new) { list }
44
+ allow(Imap::Backup::CLI::Accounts).to receive(:new) { accounts }
35
45
  allow(Imap::Backup::Account::Connection).to receive(:new) { connection }
36
46
  allow(Mail).to receive(:new) { mail }
47
+ allow(accounts).to receive(:each).and_yield(account)
37
48
  end
38
49
 
39
50
  describe "accounts" do
@@ -1,50 +1,62 @@
1
- describe Imap::Backup::CLI::Utils do
2
- let(:list) do
3
- instance_double(Imap::Backup::Configuration::List, accounts: accounts)
4
- end
5
- let(:accounts) { [{username: email}] }
6
- let(:connection) do
7
- instance_double(
8
- Imap::Backup::Account::Connection,
9
- local_folders: local_folders
10
- )
11
- end
12
- let(:local_folders) { [[serializer, folder]] }
13
- let(:folder) do
14
- instance_double(
15
- Imap::Backup::Account::Folder,
16
- exist?: true,
17
- name: "name",
18
- uid_validity: "uid_validity",
19
- uids: %w(123 456)
20
- )
21
- end
22
- let(:serializer) do
23
- instance_double(
24
- Imap::Backup::Serializer::Mbox,
25
- uids: %w(123 789),
26
- apply_uid_validity: nil,
27
- save: nil
28
- )
29
- end
30
- let(:email) { "foo@example.com" }
1
+ module Imap::Backup
2
+ describe CLI::Utils do
3
+ let(:accounts) do
4
+ instance_double(
5
+ CLI::Accounts,
6
+ find: ->(&block) { [account].find { |a| block.call(a) } }
7
+ )
8
+ end
9
+ let(:account) { instance_double(Account, username: email) }
10
+ let(:connection) do
11
+ instance_double(
12
+ Account::Connection,
13
+ account: account,
14
+ backup_folders: [folder]
15
+ )
16
+ end
17
+ let(:account) do
18
+ instance_double(
19
+ Account,
20
+ local_path: "path"
21
+ )
22
+ end
23
+ let(:folder) do
24
+ instance_double(
25
+ Account::Folder,
26
+ exist?: true,
27
+ name: "name",
28
+ uid_validity: "uid_validity",
29
+ uids: %w(123 456)
30
+ )
31
+ end
32
+ let(:serializer) do
33
+ instance_double(
34
+ Serializer::Mbox,
35
+ uids: %w(123 789),
36
+ apply_uid_validity: nil,
37
+ save: nil
38
+ )
39
+ end
40
+ let(:email) { "foo@example.com" }
31
41
 
32
- before do
33
- allow(Imap::Backup::Configuration::List).to receive(:new) { list }
34
- allow(Imap::Backup::Account::Connection).to receive(:new) { connection }
35
- end
42
+ before do
43
+ allow(CLI::Accounts).to receive(:new) { accounts }
44
+ allow(Account::Connection).to receive(:new) { connection }
45
+ allow(Serializer::Mbox).to receive(:new) { serializer }
46
+ end
36
47
 
37
- describe "ignore_history" do
38
- it "ensures the local UID validity matches the server" do
39
- subject.ignore_history(email)
48
+ describe "ignore_history" do
49
+ it "ensures the local UID validity matches the server" do
50
+ subject.ignore_history(email)
40
51
 
41
- expect(serializer).to have_received(:apply_uid_validity).with("uid_validity")
42
- end
52
+ expect(serializer).to have_received(:apply_uid_validity).with("uid_validity")
53
+ end
43
54
 
44
- it "fills the local folder with fake emails" do
45
- subject.ignore_history(email)
55
+ it "fills the local folder with fake emails" do
56
+ subject.ignore_history(email)
46
57
 
47
- expect(serializer).to have_received(:save).with("456", /From: fake@email.com/)
58
+ expect(serializer).to have_received(:save).with("456", /From: fake@email.com/)
59
+ end
48
60
  end
49
61
  end
50
62
  end
@@ -3,19 +3,21 @@ require "os"
3
3
 
4
4
  # rubocop:disable RSpec/PredicateMatcher
5
5
 
6
- describe Imap::Backup::Configuration::Store do
6
+ describe Imap::Backup::Configuration do
7
7
  let(:directory) { "/base/path" }
8
8
  let(:file_path) { File.join(directory, "/config.json") }
9
9
  let(:file_exists) { true }
10
10
  let(:directory_exists) { true }
11
- let(:data) { {debug: debug, accounts: accounts} }
12
11
  let(:debug) { true }
13
- let(:accounts) { [] }
14
12
  let(:configuration) { data.to_json }
13
+ let(:data) { {debug: debug, accounts: accounts.map(&:to_h)} }
14
+ let(:accounts) { [account1, account2] }
15
+ let(:account1) { Imap::Backup::Account.new({username: "username1"}) }
16
+ let(:account2) { Imap::Backup::Account.new({username: "username2"}) }
15
17
 
16
18
  before do
17
19
  stub_const(
18
- "Imap::Backup::Configuration::Store::CONFIGURATION_DIRECTORY", directory
20
+ "Imap::Backup::Configuration::CONFIGURATION_DIRECTORY", directory
19
21
  )
20
22
  allow(File).to receive(:directory?).with(directory) { directory_exists }
21
23
  allow(File).to receive(:exist?).and_call_original
@@ -48,8 +50,8 @@ describe Imap::Backup::Configuration::Store do
48
50
  end
49
51
 
50
52
  describe "#modified?" do
51
- context "with accounts flagged 'modified'" do
52
- let(:accounts) { [{name: "foo", modified: true}] }
53
+ context "with modified accounts" do
54
+ before { subject.accounts[0].username = "changed" }
53
55
 
54
56
  it "is true" do
55
57
  expect(subject.modified?).to be_truthy
@@ -57,7 +59,7 @@ describe Imap::Backup::Configuration::Store do
57
59
  end
58
60
 
59
61
  context "with accounts flagged 'delete'" do
60
- let(:accounts) { [{name: "foo", delete: true}] }
62
+ before { subject.accounts[0].mark_for_deletion! }
61
63
 
62
64
  it "is true" do
63
65
  expect(subject.modified?).to be_truthy
@@ -65,8 +67,6 @@ describe Imap::Backup::Configuration::Store do
65
67
  end
66
68
 
67
69
  context "without accounts flagged 'modified'" do
68
- let(:accounts) { [{name: "foo"}] }
69
-
70
70
  it "is false" do
71
71
  expect(subject.modified?).to be_falsey
72
72
  end
@@ -156,19 +156,14 @@ describe Imap::Backup::Configuration::Store do
156
156
  subject.save
157
157
  end
158
158
 
159
- context "when accounts are modified" do
160
- let(:accounts) { [{name: "foo", modified: true}] }
161
-
162
- before { subject.save }
163
-
164
- it "skips the 'modified' flag" do
165
- expected = Marshal.load(Marshal.dump(data))
166
- expected[:accounts][0].delete(:modified)
159
+ it "uses the Account#to_h method" do
160
+ allow(subject.accounts[0]).to receive(:to_h) { "Account1" }
161
+ allow(subject.accounts[1]).to receive(:to_h) { "Account2" }
167
162
 
168
- expect(JSON).to receive(:pretty_generate).with(expected)
163
+ expect(JSON).to receive(:pretty_generate).
164
+ with(hash_including({accounts: ["Account1", "Account2"]}))
169
165
 
170
- subject.save
171
- end
166
+ subject.save
172
167
  end
173
168
 
174
169
  context "when accounts are to be deleted" do
@@ -179,11 +174,15 @@ describe Imap::Backup::Configuration::Store do
179
174
  ]
180
175
  end
181
176
 
182
- it "does not save them" do
183
- expected = Marshal.load(Marshal.dump(data))
184
- expected[:accounts].pop
177
+ before do
178
+ allow(subject.accounts[0]).to receive(:to_h) { "Account1" }
179
+ allow(subject.accounts[1]).to receive(:to_h) { "Account2" }
180
+ subject.accounts[0].mark_for_deletion!
181
+ end
185
182
 
186
- expect(JSON).to receive(:pretty_generate).with(expected)
183
+ it "does not save them" do
184
+ expect(JSON).to receive(:pretty_generate).
185
+ with(hash_including({accounts: ["Account2"]}))
187
186
 
188
187
  subject.save
189
188
  end
@@ -6,7 +6,7 @@ describe Imap::Backup::Downloader do
6
6
  let(:folder) do
7
7
  instance_double(
8
8
  Imap::Backup::Account::Folder,
9
- fetch: body,
9
+ fetch_multi: [{uid: "111", body: body}],
10
10
  name: "folder",
11
11
  uids: folder_uids
12
12
  )
@@ -0,0 +1,48 @@
1
+ require "net/imap"
2
+
3
+ module Imap::Backup
4
+ describe Logger do
5
+ describe ".setup_logging" do
6
+ let(:config) { instance_double(Configuration, debug?: debug) }
7
+
8
+ around do |example|
9
+ logger_previous = described_class.logger.level
10
+ net_imap_previous = Net::IMAP.debug
11
+ described_class.logger.level = 42
12
+ Net::IMAP.debug = 42
13
+ example.run
14
+ Net::IMAP.debug = net_imap_previous
15
+ described_class.logger.level = logger_previous
16
+ end
17
+
18
+ before do
19
+ allow(Configuration).to receive(:new) { config }
20
+ described_class.setup_logging
21
+ end
22
+
23
+ context "when config.debug?" do
24
+ let(:debug) { true }
25
+
26
+ it "sets logger level to debug" do
27
+ expect(described_class.logger.level).to eq(::Logger::Severity::DEBUG)
28
+ end
29
+
30
+ it "sets the Net::IMAP debug flag" do
31
+ expect(Net::IMAP.debug).to be_a(TrueClass)
32
+ end
33
+ end
34
+
35
+ context "when not config.debug?" do
36
+ let(:debug) { false }
37
+
38
+ it "sets logger level to error" do
39
+ expect(described_class.logger.level).to eq(::Logger::Severity::ERROR)
40
+ end
41
+
42
+ it "sets the Net::IMAP debug flag" do
43
+ expect(Net::IMAP.debug).to be_a(FalseClass)
44
+ end
45
+ end
46
+ end
47
+ end
48
+ end