mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
tests and documentation.
This commit is contained in:
@@ -1,6 +1,24 @@
|
||||
mruby-dir
|
||||
=========
|
||||
|
||||
Dir class for mruby. Supported methods are:
|
||||
|
||||
`.chdir`
|
||||
`.delete`
|
||||
`.entries`
|
||||
`.exist?`
|
||||
`.foreach`
|
||||
`.getwd`
|
||||
`.mkdir`
|
||||
`.open`
|
||||
`#close`
|
||||
`#each`
|
||||
`#read`
|
||||
`#rewind`
|
||||
`#seek`
|
||||
`#tell`
|
||||
|
||||
|
||||
## License
|
||||
|
||||
Copyright (c) 2012 Internet Initiative Japan Inc.
|
||||
|
||||
+110
@@ -0,0 +1,110 @@
|
||||
# Note: testing telldir(3) and seekdir(3) in portable manner is very hard.
|
||||
|
||||
assert('Dir') do
|
||||
assert_equal(Class, Addrinfo.class)
|
||||
end
|
||||
|
||||
assert('DirTest.setup') do
|
||||
DirTest.setup
|
||||
end
|
||||
|
||||
assert('Dir.chdir') do
|
||||
assert_equal 0, Dir.chdir(DirTest.sandbox)
|
||||
end
|
||||
|
||||
assert('Dir.entries') do
|
||||
a = Dir.entries(DirTest.sandbox)
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('Dir.exist?') do
|
||||
assert_true Dir.exist?(DirTest.sandbox)
|
||||
assert_false Dir.exist?(DirTest.sandbox + "/nosuchdir")
|
||||
end
|
||||
|
||||
assert('Dir.foreach') do
|
||||
a = []
|
||||
Dir.foreach(DirTest.sandbox) { |s| a << s }
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('Dir.getwd') do
|
||||
s = Dir.getwd
|
||||
assert_true s.kind_of? String
|
||||
end
|
||||
|
||||
assert('Dir.mkdir') do
|
||||
m1 = DirTest.sandbox + "/mkdir1"
|
||||
m2 = DirTest.sandbox + "/mkdir2"
|
||||
assert_equal 0, Dir.mkdir(m1)
|
||||
assert_equal 0, Dir.mkdir(m2, 0765)
|
||||
end
|
||||
|
||||
assert('Dir.delete') do
|
||||
s = DirTest.sandbox + "/delete"
|
||||
Dir.mkdir(s)
|
||||
assert_true Dir.exist?(s)
|
||||
|
||||
Dir.delete(s)
|
||||
assert_false Dir.exist?(s)
|
||||
end
|
||||
|
||||
assert('Dir.open') do
|
||||
a = []
|
||||
Dir.open(DirTest.sandbox) { |d|
|
||||
d.each { |s| a << s }
|
||||
}
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('Dir#initialize and Dir#close') do
|
||||
d = Dir.new(".")
|
||||
assert_true d.instance_of? Dir
|
||||
assert_nil d.close
|
||||
end
|
||||
|
||||
assert('Dir#close') do
|
||||
d = Dir.new(".")
|
||||
end
|
||||
|
||||
assert('Dir#each') do
|
||||
a = []
|
||||
d = Dir.open(DirTest.sandbox)
|
||||
d.each { |s| a << s }
|
||||
d.close
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('Dir#read') do
|
||||
a = []
|
||||
d = Dir.open(DirTest.sandbox)
|
||||
while s = d.read
|
||||
a << s
|
||||
end
|
||||
d.close
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('Dir#rewind') do
|
||||
d = Dir.open(DirTest.sandbox)
|
||||
while d.read; end
|
||||
|
||||
assert_equal d, d.rewind
|
||||
|
||||
a = []
|
||||
while s = d.read
|
||||
a << s
|
||||
end
|
||||
d.close
|
||||
assert_true a.include? "a"
|
||||
assert_true a.include? "b"
|
||||
end
|
||||
|
||||
assert('DirTest.teardown') do
|
||||
DirTest.teardown
|
||||
end
|
||||
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <dirent.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#include "mruby.h"
|
||||
#include "mruby/string.h"
|
||||
#include "mruby/variable.h"
|
||||
|
||||
|
||||
mrb_value
|
||||
mrb_dirtest_setup(mrb_state *mrb, mrb_value klass)
|
||||
{
|
||||
mrb_value s;
|
||||
char buf[1024];
|
||||
const char *aname = "a";
|
||||
const char *bname = "b";
|
||||
|
||||
/* save current working directory */
|
||||
if (getcwd(buf, sizeof(buf)) == NULL) {
|
||||
mrb_raise(mrb, E_RUNTIME_ERROR, "getcwd() failed");
|
||||
}
|
||||
mrb_cv_set(mrb, klass, mrb_intern_cstr(mrb, "pwd"), mrb_str_new_cstr(mrb, buf));
|
||||
|
||||
/* create sandbox */
|
||||
snprintf(buf, sizeof(buf), "%smruby-dir-test.XXXXXX", P_tmpdir);
|
||||
if (mkdtemp(buf) == NULL) {
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdtemp(%S) failed", mrb_str_new_cstr(mrb, buf));
|
||||
}
|
||||
s = mrb_str_new_cstr(mrb, buf);
|
||||
mrb_cv_set(mrb, klass, mrb_intern_cstr(mrb, "sandbox"), s);
|
||||
|
||||
/* go to sandbox */
|
||||
if (chdir(buf) == -1) {
|
||||
rmdir(buf);
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "chdir(%S) failed", s);
|
||||
}
|
||||
|
||||
/* make some directories in the sandbox */
|
||||
if (mkdir(aname, 0) == -1) {
|
||||
chdir("..");
|
||||
rmdir(buf);
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdir(%S) failed", mrb_str_new_cstr(mrb, aname));
|
||||
}
|
||||
if (mkdir(bname, 0) == -1) {
|
||||
rmdir(aname);
|
||||
chdir("..");
|
||||
rmdir(buf);
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "mkdir(%S) failed", mrb_str_new_cstr(mrb, bname));
|
||||
}
|
||||
|
||||
return mrb_true_value();
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_dirtest_teardown(mrb_state *mrb, mrb_value klass)
|
||||
{
|
||||
mrb_value d, sandbox;
|
||||
DIR *dirp;
|
||||
struct dirent *dp;
|
||||
const char *path;
|
||||
|
||||
/* cleanup sandbox */
|
||||
sandbox = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox"));
|
||||
path = mrb_str_to_cstr(mrb, sandbox);
|
||||
|
||||
dirp = opendir(path);
|
||||
while ((dp = readdir(dirp)) != NULL) {
|
||||
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0)
|
||||
continue;
|
||||
if (rmdir(dp->d_name) == -1) {
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "rmdir(%S) failed", mrb_str_new_cstr(mrb, dp->d_name));
|
||||
}
|
||||
}
|
||||
closedir(dirp);
|
||||
|
||||
/* back to original pwd */
|
||||
d = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "pwd"));
|
||||
path = mrb_str_to_cstr(mrb, d);
|
||||
if (chdir(path) == -1) {
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "chdir(%S) failed", d);
|
||||
}
|
||||
|
||||
/* remove sandbox directory */
|
||||
sandbox = mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox"));
|
||||
path = mrb_str_to_cstr(mrb, sandbox);
|
||||
if (rmdir(path) == -1) {
|
||||
mrb_raisef(mrb, E_RUNTIME_ERROR, "rmdir(%S) failed", sandbox);
|
||||
}
|
||||
|
||||
return mrb_true_value();
|
||||
}
|
||||
|
||||
mrb_value
|
||||
mrb_dirtest_sandbox(mrb_state *mrb, mrb_value klass)
|
||||
{
|
||||
return mrb_cv_get(mrb, klass, mrb_intern_cstr(mrb, "sandbox"));
|
||||
}
|
||||
|
||||
void
|
||||
mrb_mruby_dir_gem_test(mrb_state *mrb)
|
||||
{
|
||||
struct RClass *c = mrb_define_module(mrb, "DirTest");
|
||||
|
||||
mrb_define_class_method(mrb, c, "sandbox", mrb_dirtest_sandbox, MRB_ARGS_NONE());
|
||||
mrb_define_class_method(mrb, c, "setup", mrb_dirtest_setup, MRB_ARGS_NONE());
|
||||
mrb_define_class_method(mrb, c, "teardown", mrb_dirtest_teardown, MRB_ARGS_NONE());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user