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