From c785836083bd43f88e9e1dc07d2fb328aefa950d Mon Sep 17 00:00:00 2001 From: Paul Beckingham Date: Sun, 19 Feb 2012 18:59:28 -0500 Subject: [PATCH] Feature #632 - Added feature #632, which allows environment variables TASKRC and TASKDATA to override .taskrc and .task directory locations (thanks to Steve Rader). - Added unit tests. + --- AUTHORS | 2 +- ChangeLog | 2 + doc/man/taskrc.5.in | 11 ++++++ src/Context.cpp | 20 ++++++++++ src/Context.h | 1 - src/en-US.h | 2 + test/feature.632.t | 93 +++++++++++++++++++++++++++++++++++++++++++++ 7 files changed, 129 insertions(+), 2 deletions(-) create mode 100755 test/feature.632.t diff --git a/AUTHORS b/AUTHORS index 983f958a5..9088afc15 100644 --- a/AUTHORS +++ b/AUTHORS @@ -66,6 +66,7 @@ The following submitted code, packages or analysis, and deserve special thanks: Ralph Bean Uli Martens Michal Vyskocil + Steve Rader Thanks to the following, who submitted detailed bug reports and excellent suggestions: @@ -99,7 +100,6 @@ suggestions: Max Muller Thomas Sattler Erlan Sergaziev - Steve Rader Andy Kriger Patrick R McDonald Pete Lewis diff --git a/ChangeLog b/ChangeLog index 880aeae78..ca325e5a7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -80,6 +80,8 @@ + Added feature #612, so that the 'info' command displays the sum of all active (start/stop) times for a task, if the 'journal.info' configuration variable is set (thanks to Andy Kriger). + + Added feature #632, which allows environment variables TASKRC and TASKDATA + to override .taskrc and .task directory locations (thanks to Steve Rader). + Added feature #657 & #658, using the 'ids' command, tasks matching a filter can now be modified as a group (thanks to Bryce Harrington, Eric Fluger). + Added feature #679, which makes color rules match project names in a left- diff --git a/doc/man/taskrc.5.in b/doc/man/taskrc.5.in index f0c9c1a25..e472b58e6 100644 --- a/doc/man/taskrc.5.in +++ b/doc/man/taskrc.5.in @@ -124,6 +124,17 @@ This includes two standard files that are distributed with taskwarrior, which define a set of US holidays, and set up a 16-color theme to use, to color the reports and calendar. +.SH ENVIRONMENT VARIABLES +These environmant variables override defaults and command line arguments. + +.TP +.B TASKDATA=~/.task +The overrides the default path for the taskwarrior data files. + +.TP +.B TASKRC=~/.taskrc +The overrides the default RC file. + .SH CONFIGURATION VARIABLES Valid variable names and their default values are: diff --git a/src/Context.cpp b/src/Context.cpp index a1ea0039c..c0cb1015c 100644 --- a/src/Context.cpp +++ b/src/Context.cpp @@ -91,6 +91,15 @@ int Context::initialize (int argc, const char** argv) a3.categorize (); a3.rc_override (home_dir, rc_file); + // TASKRC environment variable overrides the command line. + char* override = getenv ("TASKRC"); + if (override) + { + rc_file = File (override); + debug ("TASKRC override: "); + header (format (STRING_CONTEXT_RC_OVERRIDE, rc_file._data)); + } + // Dump any existing values and load rc file. config.clear (); config.load (rc_file); @@ -101,7 +110,18 @@ int Context::initialize (int argc, const char** argv) std::string location; a3.get_data_location (location); data_dir = Directory (location); + + override = getenv ("TASKDATA"); + if (override) + { + data_dir = Directory (override); + config.set ("data.location", data_dir._data); + header (format (STRING_CONTEXT_DATA_OVERRIDE, data_dir._data)); + } + +/* TODO Enable this when the time is right, say for 2.1 extension_dir = data_dir._data + "/extensions"; +*/ // Create missing config file and data directory, if necessary. createDefaultConfig (); diff --git a/src/Context.h b/src/Context.h index 9d6f7ee24..4baf5965b 100644 --- a/src/Context.h +++ b/src/Context.h @@ -75,7 +75,6 @@ public: private: void assumeLocations (); - void determineDataLocation (); void createDefaultConfig (); void loadAliases (); void updateXtermTitle (); diff --git a/src/en-US.h b/src/en-US.h index 392a75a23..ba2185318 100644 --- a/src/en-US.h +++ b/src/en-US.h @@ -510,6 +510,8 @@ // Context #define STRING_CONTEXT_CREATE_RC "A configuration file could not be found in {1}\n\nWould you like a sample {2} created, so taskwarrior can proceed?" #define STRING_CONTEXT_NEED_RC "Cannot proceed without rc file." +#define STRING_CONTEXT_RC_OVERRIDE "TASKRC override: {1}" +#define STRING_CONTEXT_DATA_OVERRIDE "TASKDATA override: {1}" #define STRING_CONTEXT_SHADOW_P "Configuration variable 'shadow.file' is set to " "overwrite your pending tasks. Please change it." #define STRING_CONTEXT_SHADOW_C "Configuration variable 'shadow.file' is set to " "overwrite your completed tasks. Please change it." #define STRING_CONTEXT_SHADOW_U "Configuration variable 'shadow.file' is set to " "overwrite your undo log. Please change it." diff --git a/test/feature.632.t b/test/feature.632.t new file mode 100755 index 000000000..52572bb78 --- /dev/null +++ b/test/feature.632.t @@ -0,0 +1,93 @@ +#! /usr/bin/perl +################################################################################ +## taskwarrior - a command line task list manager. +## +## Copyright 2006-2012, Paul Beckingham, Federico Hernandez. +## +## Permission is hereby granted, free of charge, to any person obtaining a copy +## of this software and associated documentation files (the "Software"), to deal +## in the Software without restriction, including without limitation the rights +## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +## copies of the Software, and to permit persons to whom the Software is +## furnished to do so, subject to the following conditions: +## +## The above copyright notice and this permission notice shall be included +## in all copies or substantial portions of the Software. +## +## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +## OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +## THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +## SOFTWARE. +## +## http://www.opensource.org/licenses/mit-license.php +## +################################################################################ + +use strict; +use warnings; +use Test::More tests => 12; +use File::Path; + +# Create the rc files. +if (open my $fh, '>', 'rc1') +{ + print $fh "data.location=./data1\n"; + close $fh; + ok (-r 'rc1', 'Created rc1'); +} + +if (open my $fh, '>', 'rc2') +{ + print $fh "data.location=./data2\n"; + close $fh; + ok (-r 'rc2', 'Created rc2'); +} + +# Feature #632: task environment variables: TASKRC and TASKDATA +qx{../src/task rc:rc1 add one}; +qx{../src/task rc:rc2 add two}; + +# All in agreement: 1 +my $output = qx{../src/task rc:rc1 list}; +like ($output, qr/one/, 'rc1'); + +$output = qx{TASKDATA=./data1 ../src/task rc:rc1 list}; +like ($output, qr/one/, 'TASKDATA, rc1'); + +$output = qx{TASKRC=./rc1 ../src/task list}; +like ($output, qr/one/, 'TASKRC'); + +$output = qx{TASKDATA=./data1 TASKRC=./rc1 ../src/task list}; +like ($output, qr/one/, 'TASKDATA, TASKRC, rc1'); + +# All in agreement: 2 +$output = qx{../src/task rc:rc2 list}; +like ($output, qr/two/, 'rc2'); + +$output = qx{TASKDATA=./data2 ../src/task rc:rc2 list}; +like ($output, qr/two/, 'TASKDATA, rc2'); + +$output = qx{TASKRC=./rc2 ../src/task list}; +like ($output, qr/two/, 'TASKRC'); + +$output = qx{TASKDATA=./data2 TASKRC=./rc2 ../src/task list}; +like ($output, qr/two/, 'TASKDATA, TASKRC, rc2'); + +# rc: overrides TASKRC, TASKDATA +$output = qx{TASKDATA=./data1 TASKRC=./rc1 ../src/task rc:rc2 list}; +like ($output, qr/one/, 'overrides TASKDATA, TASKRC override rc:'); + +rmtree ('./data1', 0 , 1); +rmtree ('./data2', 0 , 1); + +unlink qw(rc1 rc2); +ok (! -d './data1' && + ! -d './data2' && + ! -r 'rc1' && + ! -r 'rc2', 'Cleanup'); + +exit 0 +