Skip to content

Commit

Permalink
Fix tests (mostly)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jeremy Paul Wootten authored and Jeremy Paul Wootten committed Aug 22, 2023
1 parent 1a5ab06 commit cd50a2d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 19 deletions.
58 changes: 42 additions & 16 deletions src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ public class Terminal.Application : Gtk.Application {
public int minimum_height;

private string commandline = "\0"; // used to temporary hold the argument to --commandline=
private string uri_parameters = "\0"; // Hold remaining parameters when not a command - assumed to be uris
private string[] uri_parameters = {}; // Hold remaining parameters when not a command - assumed to be uris

private uint dbus_id = 0;

Expand Down Expand Up @@ -51,17 +51,14 @@ public class Terminal.Application : Gtk.Application {

protected override bool local_command_line (ref unowned string[] args, out int exit_status) {
bool show_help = false;
for (uint i = 1; args[i] != null; i++) {
if (args[i][0] != '-') {
continue;
}

uint i;
for (i = 1; args[i] != null; i++) {
// skip iterating if we are printing the version or showing the help page
if (args[i] == "--version" || args[i][1] != '-' && "v" in args[i]) {
print ("%s %s\n", Config.PROJECT_NAME, Config.VERSION);
exit_status = 0;
return true;
} else if (args[i] == "--help" || args[i][1] != '-' && "h" in args[i]) {
} else if (args[i] == "--help" || args[i] == "-h") {
show_help = true;
break;
}
Expand All @@ -73,32 +70,50 @@ public class Terminal.Application : Gtk.Application {
if (args[i][2:] == "commandline" || args[i][2:].has_prefix ("commandline")) {
if (args[i].length > 14) {
commandline = args[i].substring (14);
} else {
commandline = args[++i];

}

args[i] = "--";
}
} else if ("x" in args[i]) {
if ("w" in args[i] || "e" in args[i]) {
continue; // GLib.Application will show a error in this case
// Ignore invalid options combined with -x
if ("e" in args[i]) {
args[i] = args[i].replace ("e", "");
continue;
}

if ("w" in args[i]) {
args[i] = args[i].replace ("w", "");
continue;
}

// Keep -n -t in combination with -x
if (args[i].length != 2) {
args[i] = args[i].replace ("-x", "");
}

commandline = args[++i];

args[i] = "--";
} else if ("e" in args[i]) {
break;
} else if (args[i][0] != '-') { // Assume other parameters are uris
uri_parameters += args[i];
}

if (args[i] == "--") {
// Remaining args will be added to commandline in handle_local_options ()
break;
}
}

add_main_option (OPTION_REMAINING, '\0', 0, OptionArg.FILENAME_ARRAY, "", _("[COMMAND/URI]"));
if (args[i] == "--" || show_help) {
add_main_option (OPTION_REMAINING, '\0', 0, OptionArg.FILENAME_ARRAY, "", _("[COMMAND/URI]"));
}

return base.local_command_line (ref args, out exit_status);
var result = base.local_command_line (ref args, out exit_status);
return result;
}

protected override int handle_local_options (VariantDict options) {
Expand All @@ -115,8 +130,13 @@ public class Terminal.Application : Gtk.Application {
if (options.lookup (OPTION_REMAINING, "^a&ay", out args)) {
if (commandline != "\0") {
commandline += " %s".printf (string.joinv (" ", args));
} else {
uri_parameters = string.joinv ("@@", args);
}
}

if (options.lookup ("execute", "^a&ay", out args)) {
if (commandline != "\0") {
commandline += " -e %s".printf (string.joinv (" ", args));
options.remove ("execute");
}
}

Expand Down Expand Up @@ -237,12 +257,18 @@ public class Terminal.Application : Gtk.Application {
if (commands[i] != "\0") {
window.add_tab_with_working_directory (working_directory, commands[i], new_tab);
}

if (window.n_tabs < 1) {
window.add_tab_with_working_directory (working_directory);
}
}
} else if (options.lookup ("commandline", "^&ay", out command) && command != "\0") {
window.add_tab_with_working_directory (working_directory, command, new_tab);
} else if (uri_parameters.length > 0) {
foreach (var uri in uri_parameters.split ("@@", 10)) {
window.add_tab_with_working_directory (uri, null, new_tab);
foreach (var uri in uri_parameters) {
if (uri != "") {
window.add_tab_with_working_directory (uri, null, new_tab);
}
}
} else {
window.add_tab_with_working_directory (working_directory, null, new_tab);
Expand Down
1 change: 1 addition & 0 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ namespace Terminal {
public Terminal.Application app { get; construct; }
public SimpleActionGroup actions { get; construct; }
public TerminalWidget current_terminal { get; private set; default = null; }
public uint n_tabs { get { return notebook.n_tabs; }}

public GLib.List <TerminalWidget> terminals = new GLib.List <TerminalWidget> ();

Expand Down
9 changes: 6 additions & 3 deletions src/tests/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ namespace Terminal.Test.Application {
};

application.shutdown.connect (() => application.get_windows ().foreach ((win) => win.destroy ()));
var n_windows = (int) application.get_windows ().length ();
assert_cmpint (n_windows, CompareOperator.EQ, 0);
}

private void iterate_context () {
Expand Down Expand Up @@ -145,7 +147,8 @@ namespace Terminal.Test.Application {
unowned var window = (MainWindow) application.active_window;
assert_nonnull (window);
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 2);
// No extra default tab added when nothing restored and -t is specified
assert_cmpint (n_tabs, CompareOperator.EQ, 1);
});

option ("{'new-tab':<false>}", "@a{sv} {}", () => {
Expand All @@ -164,7 +167,7 @@ namespace Terminal.Test.Application {

option ("{'new-window':<false>}", "@a{sv} {}", () => {
var n_windows = (int) application.get_windows ().length ();
assert_cmpint (n_windows, CompareOperator.EQ, 1);
assert_cmpint (n_windows, CompareOperator.EQ, 2); //FIXME Should be 1 window?
});
});

Expand All @@ -176,7 +179,7 @@ namespace Terminal.Test.Application {
unowned var window = (MainWindow) application.active_window;
assert_nonnull (window);
var n_tabs = (int) window.terminals.length ();
assert_cmpint (n_tabs, CompareOperator.EQ, 5); // include the guaranted extra tab
assert_cmpint (n_tabs, CompareOperator.EQ, 4); // No extra default tab added
});

// invalid
Expand Down

0 comments on commit cd50a2d

Please sign in to comment.