Gentoo Archives: gentoo-commits

From: "Petteri Räty" <betelgeuse@g.o>
To: gentoo-commits@l.g.o
Subject: [gentoo-commits] proj/libbash:master commit in: src/, scripts/, /, src/builtins/tests/, src/builtins/
Date: Thu, 28 Apr 2011 06:20:07
Message-Id: 1ef836789621c8dafcd1bf7601f5050bbfc89e89.betelgeuse@gentoo
1 commit: 1ef836789621c8dafcd1bf7601f5050bbfc89e89
2 Author: Mu Qiao <qiaomuf <AT> gentoo <DOT> org>
3 AuthorDate: Thu Apr 21 13:20:15 2011 +0000
4 Commit: Petteri Räty <betelgeuse <AT> gentoo <DOT> org>
5 CommitDate: Thu Apr 28 03:09:02 2011 +0000
6 URL: http://git.overlays.gentoo.org/gitweb/?p=proj/libbash.git;a=commit;h=1ef83678
7
8 Builtin: implement source builtin
9
10 ---
11 Makefile.am | 5 +++
12 scripts/source_false.sh | 1 +
13 scripts/source_true.sh | 5 +++
14 .../source_builtin.cpp} | 38 ++++++++++++-------
15 .../source_builtin.h} | 39 ++++++++++++--------
16 .../tests/source_tests.cpp} | 35 +++++++++++-------
17 src/cppbash_builtin.cpp | 2 +
18 7 files changed, 81 insertions(+), 44 deletions(-)
19
20 diff --git a/Makefile.am b/Makefile.am
21 index aefcf51..5518599 100644
22 --- a/Makefile.am
23 +++ b/Makefile.am
24 @@ -101,6 +101,7 @@ cppunittests_SOURCES = test/run_tests.cpp \
25 src/core/tests/interpreter_test.cpp \
26 src/builtins/tests/echo_tests.cpp \
27 src/builtins/tests/boolean_tests.cpp \
28 + src/builtins/tests/source_tests.cpp \
29 test/post_check.cpp \
30 test/api_test.cpp \
31 test/walker_test.cpp
32 @@ -158,6 +159,8 @@ libcppbash_la_SOURCES = src/common.h \
33 src/builtins/echo_builtin.cpp \
34 src/builtins/echo_builtin.h \
35 src/builtins/boolean_builtins.h \
36 + src/builtins/source_builtin.h \
37 + src/builtins/source_builtin.cpp \
38 $(GENERATED_PARSER_C) \
39 $(GENERATED_PARSER_H) \
40 src/core/interpreter_exception.h \
41 @@ -182,6 +185,8 @@ EXTRA_DIST = bashast/bashast.g \
42 bashast/features_script/features.sh.tokens \
43 test/ast_printer_test.sh \
44 test/verify_bashs_test.sh \
45 + scripts/source_false.sh \
46 + scripts/source_true.sh \
47 utils/meta_gen.sh \
48 $(BASH_TESTS) \
49 $(BASH_RESULT) \
50
51 diff --git a/scripts/source_false.sh b/scripts/source_false.sh
52 new file mode 100644
53 index 0000000..c508d53
54 --- /dev/null
55 +++ b/scripts/source_false.sh
56 @@ -0,0 +1 @@
57 +false
58
59 diff --git a/scripts/source_true.sh b/scripts/source_true.sh
60 new file mode 100644
61 index 0000000..ceb5c3f
62 --- /dev/null
63 +++ b/scripts/source_true.sh
64 @@ -0,0 +1,5 @@
65 +FOO001=hello
66 +function foo()
67 +{
68 + :
69 +}
70
71 diff --git a/src/cppbash_builtin.cpp b/src/builtins/source_builtin.cpp
72 similarity index 50%
73 copy from src/cppbash_builtin.cpp
74 copy to src/builtins/source_builtin.cpp
75 index 38a6111..d2b4bcb 100644
76 --- a/src/cppbash_builtin.cpp
77 +++ b/src/builtins/source_builtin.cpp
78 @@ -17,24 +17,34 @@
79 along with libbash. If not, see <http://www.gnu.org/licenses/>.
80 */
81 ///
82 -/// \file cppbash_builtin.cpp
83 -/// \author Nathan Eloe
84 -/// \brief Implementation of class to inherit builtins from
85 +/// \file source_builtin.h
86 +/// \author Mu Qiao
87 +/// \brief class that implements the source builtin
88 ///
89
90 +#include "builtins/source_builtin.h"
91 +
92 +#include <fstream>
93 +#include <string>
94 +
95 #include "cppbash_builtin.h"
96 -#include "builtins/echo_builtin.h"
97 -#include "builtins/boolean_builtins.h"
98 +#include "core/interpreter.h"
99 +#include "core/interpreter_exception.h"
100 +#include "core/bash_ast.h"
101
102 -cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker)
103 +int source_builtin::exec(const std::vector<std::string>& bash_args)
104 {
105 -}
106 + if(bash_args.size() == 0)
107 + throw interpreter_exception("should provide one argument for source builtin");
108 +
109 + // we need fix this to pass extra arguments as positional parameters
110 + const std::string& path = bash_args[0];
111 + std::ifstream input(path);
112 + if(!input)
113 + throw interpreter_exception(path + " can't be read");
114 +
115 + bash_ast ast(input);
116 + ast.interpret_with(_walker);
117
118 -cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
119 - static boost::scoped_ptr<builtins_type> p(new builtins_type {
120 - {"echo", boost::factory<echo_builtin*>()},
121 - {"true", boost::factory<true_builtin*>()},
122 - {"false", boost::factory<false_builtin*>()}
123 - });
124 - return *p;
125 + return _walker.get_status();
126 }
127
128 diff --git a/src/cppbash_builtin.cpp b/src/builtins/source_builtin.h
129 similarity index 55%
130 copy from src/cppbash_builtin.cpp
131 copy to src/builtins/source_builtin.h
132 index 38a6111..964d214 100644
133 --- a/src/cppbash_builtin.cpp
134 +++ b/src/builtins/source_builtin.h
135 @@ -17,24 +17,31 @@
136 along with libbash. If not, see <http://www.gnu.org/licenses/>.
137 */
138 ///
139 -/// \file cppbash_builtin.cpp
140 -/// \author Nathan Eloe
141 -/// \brief Implementation of class to inherit builtins from
142 +/// \file source_builtin.h
143 +/// \author Mu Qiao
144 +/// \brief class that implements the source builtin
145 ///
146
147 +#ifndef LIBBASH_BUILTINS_SOURCE_BUILTIN_H_
148 +#define LIBBASH_BUILTINS_SOURCE_BUILTIN_H_
149 +
150 #include "cppbash_builtin.h"
151 -#include "builtins/echo_builtin.h"
152 -#include "builtins/boolean_builtins.h"
153
154 -cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker)
155 +///
156 +/// \class source_builtin
157 +/// \brief the source builtin for bash
158 +///
159 +class source_builtin: public virtual cppbash_builtin
160 {
161 -}
162 -
163 -cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
164 - static boost::scoped_ptr<builtins_type> p(new builtins_type {
165 - {"echo", boost::factory<echo_builtin*>()},
166 - {"true", boost::factory<true_builtin*>()},
167 - {"false", boost::factory<false_builtin*>()}
168 - });
169 - return *p;
170 -}
171 + public:
172 + BUILTIN_CONSTRUCTOR(source)
173 +
174 + ///
175 + /// \brief runs the source builtin on the supplied arguments
176 + /// \param bash_args the arguments to the source builtin
177 + /// \return exit status of source
178 + ///
179 + virtual int exec(const std::vector<std::string>& bash_args);
180 +};
181 +
182 +#endif
183
184 diff --git a/src/cppbash_builtin.cpp b/src/builtins/tests/source_tests.cpp
185 similarity index 51%
186 copy from src/cppbash_builtin.cpp
187 copy to src/builtins/tests/source_tests.cpp
188 index 38a6111..ac0fd3c 100644
189 --- a/src/cppbash_builtin.cpp
190 +++ b/src/builtins/tests/source_tests.cpp
191 @@ -17,24 +17,31 @@
192 along with libbash. If not, see <http://www.gnu.org/licenses/>.
193 */
194 ///
195 -/// \file cppbash_builtin.cpp
196 -/// \author Nathan Eloe
197 -/// \brief Implementation of class to inherit builtins from
198 +/// \file source_tests.cpp
199 +/// \brief series of unit tests for source built in
200 ///
201
202 +#include <cstdlib>
203 +
204 +#include <iostream>
205 +#include <string>
206 +
207 +#include <gtest/gtest.h>
208 +
209 +#include "core/interpreter.h"
210 #include "cppbash_builtin.h"
211 -#include "builtins/echo_builtin.h"
212 -#include "builtins/boolean_builtins.h"
213
214 -cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker)
215 +using namespace std;
216 +
217 +TEST(source_builtin_test, source)
218 {
219 -}
220 + std::string srcdir(getenv("srcdir"));
221 + interpreter walker;
222 + int status = cppbash_builtin::exec("source", {srcdir + "/scripts/source_true.sh"}, std::cout, std::cerr, std::cin, walker);
223 + EXPECT_EQ(status, 0);
224 + EXPECT_TRUE(walker.has_function("foo"));
225 + EXPECT_STREQ("hello", walker.resolve<std::string>("FOO001").c_str());
226
227 -cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
228 - static boost::scoped_ptr<builtins_type> p(new builtins_type {
229 - {"echo", boost::factory<echo_builtin*>()},
230 - {"true", boost::factory<true_builtin*>()},
231 - {"false", boost::factory<false_builtin*>()}
232 - });
233 - return *p;
234 + status = cppbash_builtin::exec("source", {srcdir + "/scripts/source_false.sh"}, std::cout, std::cerr, std::cin, walker);
235 + EXPECT_EQ(status, 1);
236 }
237
238 diff --git a/src/cppbash_builtin.cpp b/src/cppbash_builtin.cpp
239 index 38a6111..0df9cbf 100644
240 --- a/src/cppbash_builtin.cpp
241 +++ b/src/cppbash_builtin.cpp
242 @@ -25,6 +25,7 @@
243 #include "cppbash_builtin.h"
244 #include "builtins/echo_builtin.h"
245 #include "builtins/boolean_builtins.h"
246 +#include "builtins/source_builtin.h"
247
248 cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&err), _inp_stream(&in), _walker(walker)
249 {
250 @@ -33,6 +34,7 @@ cppbash_builtin::cppbash_builtin(BUILTIN_ARGS): _out_stream(&out), _err_stream(&
251 cppbash_builtin::builtins_type& cppbash_builtin::builtins() {
252 static boost::scoped_ptr<builtins_type> p(new builtins_type {
253 {"echo", boost::factory<echo_builtin*>()},
254 + {"source", boost::factory<source_builtin*>()},
255 {"true", boost::factory<true_builtin*>()},
256 {"false", boost::factory<false_builtin*>()}
257 });