From 15bface10d2ebaac3f0a7b01da2c7be285a61807 Mon Sep 17 00:00:00 2001 From: Michael Stapelberg Date: Wed, 19 Oct 2011 19:57:39 +0100 Subject: [PATCH] Create different IDs for each bar (+test) --- src/cfgparse.y | 9 ++++++++- src/main.c | 2 ++ testcases/t/177-bar-config.t | 39 ++++++++++++++++++++++++++++++++++-- 3 files changed, 47 insertions(+), 3 deletions(-) diff --git a/src/cfgparse.y b/src/cfgparse.y index 6e59c87c..2c796af1 100644 --- a/src/cfgparse.y +++ b/src/cfgparse.y @@ -934,7 +934,14 @@ bar: { printf("\t new bar configuration finished, saving.\n"); /* Generate a unique ID for this bar */ - current_bar.id = sstrdup("foo"); /* TODO */ + current_bar.id = sstrdup("bar-XXXXXX"); + /* This works similar to mktemp in that it replaces the last six X with + * random letters, but without the restriction that the given buffer + * has to contain a valid path name. */ + char *x = current_bar.id + strlen("bar-"); + while (*x != '\0') { + *(x++) = (rand() % 26) + 'a'; + } /* Copy the current (static) structure into a dynamically allocated * one, then cleanup our static one. */ diff --git a/src/main.c b/src/main.c index ffdc1fd3..832d7f1b 100644 --- a/src/main.c +++ b/src/main.c @@ -217,6 +217,8 @@ int main(int argc, char *argv[]) { if (!isatty(fileno(stdout))) setbuf(stdout, NULL); + srand(time(NULL)); + init_logging(); start_argv = argv; diff --git a/testcases/t/177-bar-config.t b/testcases/t/177-bar-config.t index 98eb4edd..c7cf2843 100644 --- a/testcases/t/177-bar-config.t +++ b/testcases/t/177-bar-config.t @@ -99,9 +99,9 @@ $i3 = i3(get_socket_path(0)); $bars = $i3->get_bar_config()->recv; is(@$bars, 1, 'one bar configured'); -my $bar_id = shift @$bars; +$bar_id = shift @$bars; -my $bar_config = $i3->get_bar_config($bar_id)->recv; +$bar_config = $i3->get_bar_config($bar_id)->recv; is($bar_config->{status_command}, 'i3status --bar', 'status_command correct'); ok($bar_config->{verbose}, 'verbose on'); ok(!$bar_config->{workspace_buttons}, 'workspace buttons disabled'); @@ -127,4 +127,39 @@ is_deeply($bar_config->{colors}, exit_gracefully($pid); +##################################################################### +# ensure that multiple bars get different IDs +##################################################################### + +$config = <get_bar_config()->recv; +is(@$bars, 2, 'two bars configured'); +isnt($bars->[0], $bars->[1], 'bar IDs are different'); + +my $bar1_config = $i3->get_bar_config($bars->[0])->recv; +my $bar2_config = $i3->get_bar_config($bars->[1])->recv; + +isnt($bar1_config->{outputs}, $bar2_config->{outputs}, 'outputs different'); + +exit_gracefully($pid); + done_testing; -- 2.39.5