From df6be44b61fb6cab6bb1dea58ac6f51ac4256a53 Mon Sep 17 00:00:00 2001 From: Diego Barros Date: Wed, 8 Oct 2014 18:26:26 +1100 Subject: [PATCH] Handle shell operations with spaces and quotes Fixed: Running a shell command that has an argument with spaces and is inside quotes would fail. --- Slate/ShellOperation.m | 2 +- SlateTests/TestShellUtils.m | 8 ++++++++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/Slate/ShellOperation.m b/Slate/ShellOperation.m index d73d4f19..35a45cbb 100644 --- a/Slate/ShellOperation.m +++ b/Slate/ShellOperation.m @@ -133,7 +133,7 @@ + (id)shellOperationFromString:(NSString *)shellOperation { } NSString *commandAndArgs = [tokens lastObject]; NSMutableArray *commandAndArgsTokens = [NSMutableArray array]; - [StringTokenizer tokenize:commandAndArgs into:commandAndArgsTokens]; + [StringTokenizer tokenize:commandAndArgs into:commandAndArgsTokens quoteChars:[NSCharacterSet characterSetWithCharactersInString:QUOTES]]; if ([commandAndArgsTokens count] < 1) { SlateLogger(@"ERROR: Invalid Parameters '%@'", shellOperation); @throw([NSException exceptionWithName:@"Invalid Parameters" reason:[NSString stringWithFormat:@"Invalid Parameters in '%@'. Shell operations require the following format: shell [wait] 'command'", shellOperation] userInfo:nil]); diff --git a/SlateTests/TestShellUtils.m b/SlateTests/TestShellUtils.m index ce5f008e..99d47351 100644 --- a/SlateTests/TestShellUtils.m +++ b/SlateTests/TestShellUtils.m @@ -20,6 +20,7 @@ #import "TestShellUtils.h" #import "ShellUtils.h" +#import "ShellOperation.h" @implementation TestShellUtils @@ -53,4 +54,11 @@ - (void)testRunWithQuotedArgs { STAssertEquals(found, 1, @"Result should include all strings"); } +-(void)testShellOperationFromStringWithQuotesAndSpaces { + id operation = [ShellOperation shellOperationFromString:@"shell '/usr/bin/open -a \"Sublime Text\"'"]; + int argumentCount = [[operation args] count]; + + STAssertEquals(argumentCount, 2, @"The shell operation must only contain two arguments"); +} + @end