request-pull: rewrite to C
[git] / t / t10000-ruby.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2013 Felipe Contreras
4 #
5
6 test_description='test ruby support'
7
8 . ./test-lib.sh
9
10 test_expect_success 'basic support' '
11         git ruby > actual <<-EOF &&
12         puts "hello world"
13         EOF
14         echo "hello world" > expected &&
15         test_cmp expected actual
16 '
17
18 test_expect_success 'argument passing' '
19         cat > script <<-"EOF" &&
20         p($0)
21         p(ARGV)
22         EOF
23         git ruby script foo bar > actual &&
24         cat > expected <<-EOF &&
25         "script"
26         ["foo", "bar"]
27         EOF
28         test_cmp expected actual
29 '
30
31 test_expect_success 'test for_each_ref()' '
32         test_commit foo &&
33         git ruby > actual <<-EOF &&
34         for_each_ref() do |name, sha1, flags|
35                 puts "%s: %s" % [name, sha1_to_hex(sha1)]
36         end
37         EOF
38         git for-each-ref --format="%(refname): %(objectname)" > expected &&
39         test_cmp expected actual
40 '
41
42 test_expect_success 'test setup_git_directory()' '
43         mkdir t &&
44         (
45         cd t &&
46         git ruby > ../actual <<-EOF
47         prefix, nongit_ok = setup_git_directory()
48         puts prefix
49         EOF
50         ) &&
51         echo "t/" > expected &&
52         test_cmp expected actual
53 '
54
55 test_expect_success 'test dwim_ref()' '
56         git ruby > actual <<-EOF &&
57         sha1, num, ref = dwim_ref("HEAD")
58         puts sha1_to_hex(sha1), num, ref
59         EOF
60         git rev-parse -q --verify HEAD > expected &&
61         echo 1 >> expected &&
62         git rev-parse -q --verify --symbolic-full-name HEAD >> expected &&
63         test_cmp expected actual
64 '
65
66 test_expect_success 'test git_config()' '
67         git ruby > actual <<-EOF &&
68         git_config() do |key, value|
69           puts "%s=%s" % [key, value]
70         end
71         EOF
72         git config --list > expected &&
73         test_cmp expected actual
74 '
75
76 test_expect_success 'test get_sha1()' '
77         git ruby > actual <<-EOF &&
78         puts sha1_to_hex(get_sha1("HEAD"))
79         EOF
80         git rev-parse -q --verify HEAD > expected &&
81         test_cmp expected actual
82 '
83
84 test_expect_success 'test Object' '
85         git ruby > actual <<-EOF &&
86         object = Git::Object.get(get_sha1("HEAD"))
87         puts object, object.type == OBJ_COMMIT, sha1_to_hex(object.sha1)
88         EOF
89         git rev-parse -q --verify HEAD > expected &&
90         echo "true" >> expected &&
91         git rev-parse -q --verify HEAD >> expected &&
92         test_cmp expected actual
93 '
94
95 test_expect_success 'test Commit' '
96         git ruby > actual <<-EOF &&
97         commit = Git::Commit.get(get_sha1("HEAD"))
98         puts commit, commit.buffer
99         EOF
100         git rev-parse -q --verify HEAD > expected &&
101         git cat-file commit HEAD >> expected &&
102         test_cmp expected actual
103 '
104
105 test_expect_success 'test ParseOpt' '
106         cat > parse-script <<"EOF"
107         $str = "default"
108         $num = 0
109         $bool = false
110
111         opts = ParseOpt.new
112         opts.usage = "git foo"
113
114         opts.on("b", "bool", help: "Boolean") do |v|
115           $bool = v
116         end
117
118         opts.on("s", "string", help: "String") do |v|
119           $str = v
120         end
121
122         opts.on("n", "number", help: "Number") do |v|
123           $num = v.to_i
124         end
125
126         opts.parse
127
128         p(ARGV)
129         p({ :bool => $bool, :str => $str, :num => $num })
130         EOF
131
132         git ruby parse-script > actual &&
133         cat > expected <<-EOF &&
134         []
135         {:bool=>false, :str=>"default", :num=>0}
136         EOF
137         test_cmp expected actual &&
138
139         git ruby parse-script --bool > actual &&
140         cat > expected <<-EOF &&
141         []
142         {:bool=>true, :str=>"default", :num=>0}
143         EOF
144         test_cmp expected actual &&
145
146         git ruby parse-script -b > actual &&
147         cat > expected <<-EOF &&
148         []
149         {:bool=>true, :str=>"default", :num=>0}
150         EOF
151         test_cmp expected actual &&
152
153         git ruby parse-script --string=foo > actual &&
154         cat > expected <<-EOF &&
155         []
156         {:bool=>false, :str=>"foo", :num=>0}
157         EOF
158         test_cmp expected actual &&
159
160         git ruby parse-script -sfoo > actual &&
161         cat > expected <<-EOF &&
162         []
163         {:bool=>false, :str=>"foo", :num=>0}
164         EOF
165         test_cmp expected actual &&
166
167         git ruby parse-script --number=10 > actual &&
168         cat > expected <<-EOF &&
169         []
170         {:bool=>false, :str=>"default", :num=>10}
171         EOF
172         test_cmp expected actual &&
173
174         git ruby parse-script --bool --string=bar --number=-20 > actual &&
175         cat > expected <<-EOF &&
176         []
177         {:bool=>true, :str=>"bar", :num=>-20}
178         EOF
179         test_cmp expected actual &&
180
181         git ruby parse-script --help > actual &&
182         cat > expected <<-EOF &&
183         usage: git foo
184             -b, --bool            Boolean
185             -s, --string          String
186             -n, --number          Number
187         EOF
188         test_cmp expected actual &&
189
190         git ruby parse-script --help > actual &&
191         cat > expected <<-EOF &&
192         usage: git foo
193             -b, --bool            Boolean
194             -s, --string          String
195             -n, --number          Number
196         EOF
197         test_cmp expected actual &&
198
199         test_must_fail git ruby parse-script --bad > actual &&
200         cat > expected <<-EOF &&
201         usage: git foo
202             -b, --bool            Boolean
203             -s, --string          String
204             -n, --number          Number
205         EOF
206         test_cmp expected actual &&
207
208         git ruby parse-script one --bool two --string=bar three --number=-20 mambo > actual &&
209         cat > expected <<-EOF &&
210         ["one", "two", "three", "mambo"]
211         {:bool=>true, :str=>"bar", :num=>-20}
212         EOF
213         test_cmp expected actual &&
214
215         git ruby parse-script one --bool two -- --three four > actual &&
216         cat > expected <<-EOF &&
217         ["one", "two", "--three", "four"]
218         {:bool=>true, :str=>"default", :num=>0}
219         EOF
220         test_cmp expected actual &&
221
222         git ruby parse-script one --bool --no-bool > actual &&
223         cat > expected <<-EOF &&
224         ["one"]
225         {:bool=>false, :str=>"default", :num=>0}
226         EOF
227         cat actual
228         test_cmp expected actual &&
229
230         true
231 '
232
233 test_done