test/harness/testcaseio.c
author ali <ali@juiblex.co.uk>
Sat Oct 26 18:47:33 2013 +0100 (2013-10-26)
changeset 101 f44c530f80da
parent 17 2c88fd553e5d
child 102 ff0aa9b1397a
permissions -rw-r--r--
Fix bug #24: Accept alternate form of newline
     1 #include <stdlib.h>
     2 #include <stdio.h>
     3 #include <string.h>
     4 #include <glib.h>
     5 #include <bl/bl.h>
     6 #include "testcaseparser.h"
     7 #include "testcaseinput.h"
     8 #include "testcaseio.h"
     9 #include "warningsparser.h"
    10 
    11 /*
    12  * Read a testcase in from a file.
    13  * On error, print a suitable message using g_printerr and return NULL.
    14  * The returned testcase should be freed with testcase_free().
    15  */
    16 Testcase *testcase_parse_file(const char *filename)
    17 {
    18     Testcase *testcase;
    19     TestcaseParser *parser;
    20     TestcaseInput *input=NULL;
    21     GMarkupParseContext *context;
    22     GError *err=NULL;
    23     char *s,*arg;
    24     const char *tag,*text;
    25     gboolean found_tag=FALSE,newlines_set=FALSE;
    26     parser=testcase_parser_new_from_file(filename);
    27     if (!parser)
    28 	return NULL;
    29     if (!*testcase_parser_get_flag(parser))
    30     {
    31 	g_printerr("%s: Not a valid testcase (flag)\n",filename);
    32 	testcase_parser_free(parser);
    33 	return NULL;
    34     }
    35     testcase=g_new0(Testcase,1);
    36     testcase->basename=g_path_get_basename(filename);
    37     s=strrchr(testcase->basename,'.');
    38     if (s)
    39 	*s='\0';
    40     while(testcase_parser_get_next_tag(parser,&tag,&text))
    41     {
    42 	if (!input && !strcmp(tag,"INPUT"))
    43 	    input=testcase_input_new("TEST-XXXXXX",text);
    44 	else if (g_str_has_prefix(tag,"INPUT(") && tag[strlen(tag)-1]==')')
    45 	{
    46 	    arg=g_strndup(tag+6,strlen(tag)-7);
    47 	    s=g_path_get_dirname(arg);
    48 	    if (strcmp(s,"."))
    49 	    {
    50 		/*
    51 		 * Otherwise it would be possible to overwrite an arbitary
    52 		 * file on somebody's computer by getting them to run a
    53 		 * testcase!
    54 		 */
    55 		g_printerr(
    56 		  "%s: Input files may not have a directory component\n",arg);
    57 		g_free(s);
    58 		g_free(arg);
    59 		testcase_free(testcase);
    60 		testcase_parser_free(parser);
    61 		return NULL;
    62 	    }
    63 	    g_free(s);
    64 	    testcase->inputs=g_slist_prepend(testcase->inputs,
    65 	      testcase_input_new(arg,text));
    66 	    if (!strstr(arg,"XXXXXX"))
    67 		testcase->flags|=TESTCASE_TMP_DIR;
    68 	    g_free(arg);
    69 	}
    70 	else if (!testcase->expected && !testcase->warnings &&
    71 	  !strcmp(tag,"EXPECTED"))
    72 	    testcase->expected=g_strdup(text);
    73 	else if (!testcase->expected && !testcase->warnings &&
    74 	  !strcmp(tag,"WARNINGS"))
    75 	{
    76 	    context=warnings_parse_context_new(testcase);
    77 	    if (!g_markup_parse_context_parse(context,text,-1,&err) ||
    78 	      !g_markup_parse_context_end_parse(context,&err))
    79 	    {
    80 		g_markup_parse_context_free(context);
    81 		g_printerr("%s\n",err->message);
    82 		g_clear_error(&err);
    83 		testcase_free(testcase);
    84 		testcase_parser_free(parser);
    85 		return NULL;
    86 	    }
    87 	    g_markup_parse_context_free(context);
    88 	}
    89 	else if (!testcase->encoding && !strcmp(tag,"ENCODING"))
    90 	    testcase->encoding=g_strchomp(g_strdup(text));
    91 	else if (!newlines_set && !strcmp(tag,"NEWLINES"))
    92 	{
    93 	    newlines_set=TRUE;
    94 	    s=g_strdup(text);
    95 	    g_strchomp(s);
    96 	    if (!strcmp(s,"LF"))
    97 		testcase->flags|=TESTCASE_UNIX_NEWLINES;
    98 	    else if (!strcmp(s,"CR"))
    99 		testcase->flags|=TESTCASE_OS9_NEWLINES;
   100 	    else if (strcmp(s,"CRLF"))
   101 	    {
   102 		g_printerr(
   103 		  "%s: Unrecognised style for newlines. Try CR or LF.\n",s);
   104 		g_free(s);
   105 		testcase_free(testcase);
   106 		testcase_parser_free(parser);
   107 		return NULL;
   108 	    }
   109 	    g_free(s);
   110 	}
   111 	else if (!testcase->encoding && !strcmp(tag,"OPTIONS"))
   112 	{
   113 	    testcase->options=g_strsplit(text,"\n",0);
   114 	    g_free(testcase->options[g_strv_length(testcase->options)-1]);
   115 	    testcase->options[g_strv_length(testcase->options)-1]=NULL;
   116 	}
   117 	else
   118 	{
   119 	    g_printerr("%s: Not a valid testcase (%s)\n",filename,tag);
   120 	    testcase_free(testcase);
   121 	    testcase_parser_free(parser);
   122 	    return NULL;
   123 	}
   124 	found_tag=TRUE;
   125     }
   126     if (!testcase_parser_at_eof(parser))
   127     {
   128 	if (found_tag)
   129 	    g_printerr("%s: Not a valid testcase (garbage at end)\n",
   130 	      filename);
   131 	else
   132 	    g_printerr("%s: Not a valid testcase (no valid tags)\n",
   133 	      filename);
   134 	testcase_free(testcase);
   135 	testcase_parser_free(parser);
   136 	return NULL;
   137     }
   138     if (!input)
   139 	input=testcase_input_new("TEST-XXXXXX",NULL);
   140     testcase->inputs=g_slist_prepend(testcase->inputs,input);
   141     testcase_parser_free(parser);
   142     return testcase;
   143 }