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