Make both Engine and AccountInfo a bit more unit testable.

* src/engine/api/geary-account-information.vala (AccountInformation):
  Make main constrcutor public, pass in the id rather than divining it
  from the config dir's name for both ctors. Update call sites.

* src/engine/api/geary-engine.vala (Engine): Also make the ctor public.
This commit is contained in:
Michael James Gratton 2017-02-17 17:39:05 +11:00
parent dbf0251646
commit 6eac43b2e7
2 changed files with 14 additions and 11 deletions

View file

@ -209,8 +209,8 @@ public class Geary.AccountInformation : BaseObject {
/**
* Creates a new, empty account info file.
*/
internal AccountInformation(File config_directory, File data_directory) {
this.id = config_directory.get_basename();
public AccountInformation(string id, File config_directory, File data_directory) {
this.id = id;
this.config_dir = config_directory;
this.data_dir = data_directory;
this.file = config_dir.get_child(SETTINGS_FILENAME);
@ -222,10 +222,11 @@ public class Geary.AccountInformation : BaseObject {
* Throws an error if the config file was not found, could not be
* parsed, or doesn't have all required fields.
*/
internal AccountInformation.from_file(File config_directory,
internal AccountInformation.from_file(string id,
File config_directory,
File data_directory)
throws Error {
this(config_directory, data_directory);
this(id, config_directory, data_directory);
KeyFile key_file = new KeyFile();
key_file.load_from_file(file.get_path() ?? "", KeyFileFlags.NONE);

View file

@ -105,10 +105,11 @@ public class Geary.Engine : BaseObject {
*/
public signal void untrusted_host(Geary.AccountInformation account_information,
Endpoint endpoint, Endpoint.SecurityType security, TlsConnection cx, Service service);
private Engine() {
// Public so it can be tested
public Engine() {
}
private void check_opened() throws EngineError {
if (!is_open)
throw new EngineError.OPEN_REQUIRED("Geary.Engine instance not open");
@ -192,10 +193,12 @@ public class Geary.Engine : BaseObject {
FileInfo info = info_list.nth_data(0);
if (info.get_file_type() == FileType.DIRECTORY) {
try {
string id = info.get_name();
account_list.add(
new AccountInformation.from_file(
user_config_dir.get_child(info.get_name()),
user_data_dir.get_child(info.get_name())
id,
user_config_dir.get_child(id),
user_data_dir.get_child(id)
)
);
} catch (Error err) {
@ -291,8 +294,7 @@ public class Geary.Engine : BaseObject {
throw new EngineError.ALREADY_EXISTS("Account %s already exists", id);
return new AccountInformation(
user_config_dir.get_child(id),
user_data_dir.get_child(id)
id, user_config_dir.get_child(id), user_data_dir.get_child(id)
);
}