about summary refs log tree commit diff
path: root/pkgs/games/build-support/monogame-patcher/src/test.sh
blob: b7380f273a38c8c3b1d19392a6066668456a83cf (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
set -x
cd "$(mktemp -d)"

cat > "a.cs" <<EOF
using System;

public class a {
    public static string replaceMe(string foo) {
        Console.WriteLine("foo called: " + foo);
        return "foo";
    }
}
EOF

cat > "b.cs" <<EOF
using System;
using System.IO;

public class b {
    public static string replacement(string bar) {
        if (bar == "nope")
            return "nope";
        Console.WriteLine("bar called: " + bar);
        return "bar";
    }

    public static void wrongFileStreamUse() {
        var fs = new FileStream("write_test.txt", FileMode.Open);
        if (fs.CanWrite)
            Console.WriteLine("can write");
        else
            Console.WriteLine("can not write");
    }
}
EOF

cat > "c.cs" <<EOF
using System;

public class c {
    public static string anotherReplacement(string foobar) {
        if (foobar == "nope")
            return "nope";
        Console.WriteLine("foobar called: " + foobar);
        return "foobar";
    }
}
EOF

cat > "test1.cs" <<EOF
class test1 {
    public static void Main() {
        a.replaceMe("xxx");
        b.replacement("nope");
    }
}
EOF

cat > "test2.cs" <<EOF
class test1 {
    public static void Main() {
        b.wrongFileStreamUse();
    }
}
EOF

mkdir subdir
mcs a.cs -target:library -out:subdir/a.dll
mcs b.cs -target:library -out:subdir/b.dll
mcs c.cs -target:library -out:subdir/c.dll

mcs test1.cs -r:subdir/a -r:subdir/b -out:subdir/test1.exe
mcs test2.cs -r:subdir/a -r:subdir/b -out:subdir/test2.exe

! "$out/bin/monogame-patcher" replace-call -i subdir/test1.exe \
    "System.String a::replaceMe(System.String)" \
    "System.String b::notfound(System.String)" \
    test1 2> /dev/null

"$out/bin/monogame-patcher" replace-call -i subdir/test1.exe \
    "System.String a::replaceMe(System.String)" \
    "System.String b::replacement(System.String)" \
    test1

test "$(mono subdir/test1.exe)" = "bar called: xxx"

"$out/bin/monogame-patcher" replace-call -i subdir/test1.exe -a subdir/c.dll \
    "System.String b::replacement(System.String)" \
    "System.String c::anotherReplacement(System.String)" \
    test1

test "$(mono subdir/test1.exe)" = "foobar called: xxx"

echo foo > write_test.txt

test "$(mono subdir/test2.exe)" = "can write"

"$out/bin/monogame-patcher" fix-filestreams -i subdir/b.dll b

test "$(mono subdir/test2.exe)" = "can not write"

set +e
"$out/bin/monogame-patcher" --help &> /dev/null
ret=$?
set -e
if [ $ret -eq 0 ]; then
    echo "Running with --help should give exit status != 0 but was $ret" >&2
    exit 1
fi

"$out/bin/monogame-patcher" --help 2>&1 | grep -q fix-filestreams