knut_tools 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,67 @@
1
+ =begin rdoc
2
+ Modify Rake::TestTask to allow tests in different directories.
3
+
4
+ This may be necessary, if you have to test, if files are created correct.
5
+
6
+ Details see Rake::TestTask
7
+ =end
8
+
9
+ #~ gem 'rake', '=0.8.7'
10
+ #~ gem 'rake', '>= 0.8.7'
11
+ #~ gem 'rake', '=0.8'
12
+
13
+ require 'rake/testtask.rb'
14
+ module Rake
15
+ =begin rdoc
16
+ Modify Rake::TestTask to allow tests in different directories.
17
+
18
+ This may be necessary, if you have to test, if files are created correct.
19
+
20
+ Example:
21
+ Rake::TestTask.new(:test_ok) do |t|
22
+ t.dir = $testdir
23
+ t.test_files = $testfile
24
+ t.verbose = true
25
+ end
26
+ $testfile must be located relative to $testdir.
27
+ =end
28
+ class TestTask
29
+ # Directory to find and start the tests.
30
+ attr_accessor :dir
31
+ =begin rdoc
32
+ Create the tasks defined by this task lib.
33
+
34
+ Redefined to run the tests from another directory.
35
+ The original definition was rake version 0.8.7.
36
+
37
+ =end
38
+ def define
39
+ lib_path = @libs.join(File::PATH_SEPARATOR)
40
+ desc "Run tests" + (@name==:test ? "" : " for #{@name}")
41
+ task @name do
42
+ run_code = ''
43
+ RakeFileUtils.verbose(@verbose) do
44
+ run_code =
45
+ case @loader
46
+ when :direct
47
+ "-e 'ARGV.each{|f| load f}'"
48
+ when :testrb
49
+ "-S testrb #{fix}"
50
+ when :rake
51
+ rake_loader
52
+ end
53
+ @ruby_opts.unshift( "-I\"#{lib_path}\"" )
54
+ @ruby_opts.unshift( "-w" ) if @warning
55
+ #Change to directory if requested.
56
+ Dir.chdir(@dir ? @dir : '.'){ # ##add knut##
57
+ ruby @ruby_opts.join(" ") +
58
+ " \"#{run_code}\" " +
59
+ file_list.collect { |fn| "\"#{fn}\"" }.join(' ') +
60
+ " #{option_list}"
61
+ } # ##add knut##
62
+ end
63
+ end
64
+ self
65
+ end
66
+ end #TestTask
67
+ end
@@ -0,0 +1,78 @@
1
+ =begin rdoc
2
+ Redefine require to get a list with all expanded filenames
3
+ of the required features.
4
+
5
+ In ruby standard, $LOADED_FEATURES ($") is
6
+ an array containing the filenames of modules loaded by require.
7
+
8
+ This script fills also $LOADED_FEATURES_FULL with the full pathes of
9
+ modules loaded by require.
10
+
11
+ $LOADED_FEATURES_WARN contains warnings with conflicts.
12
+
13
+ =Example
14
+ Just imagine you have strange errors when you use singleton.
15
+ You call "require 'singleton'", but no Singleton is defined.
16
+
17
+ Ok, the problem may be easy, there is anywhere a 'singleton.rb' - but where?
18
+
19
+ Just add 'knut_tools/required_what' before you load your package,
20
+ then analyse $LOADED_FEATURES_FULL and $LOADED_FEATURES_WARN:
21
+
22
+ require 'knut_tools/required_what'
23
+ require 'singleton'
24
+
25
+ puts $LOADED_FEATURES_FULL #The loaded gems with full path
26
+ puts $LOADED_FEATURES_WARN #Possible name conflicts
27
+
28
+ In $LOADED_FEATURES_FULL you see, whats loaded really, not what you expected.
29
+ $LOADED_FEATURES_WARN contains a list with warnings.
30
+ =end
31
+
32
+ =begin rdoc
33
+ An 'expanded $LOADED_FEATURES ($")'.
34
+ Contains all loaded features, but with the complete path.
35
+ =end
36
+ $LOADED_FEATURES_FULL = []
37
+
38
+ =begin rdoc
39
+ Collects all warnings about loading conflicts.
40
+ =end
41
+ $LOADED_FEATURES_WARN = []
42
+
43
+
44
+ =begin rdoc
45
+ Store require to use it in the redefinition.
46
+ =end
47
+ alias :old_require :require
48
+
49
+ =begin rdoc
50
+ Redefine require, to get an 'expanded $"' ($LOADED_FEATURES_FULL)
51
+ All loaded features, but with the complete path.
52
+
53
+ If there is a name conflict between the files you load,
54
+ a warning is collected in
55
+ =end
56
+ def require( package )
57
+ #make the original require and return if there was a failure.
58
+ return false unless old_require package
59
+ loaded = $LOADED_FEATURES.last #get the filename of the last load
60
+ found = [] #help variable to collect possible file pathes.
61
+ #loop on search pathes
62
+ $LOAD_PATH.each{|searchpath|
63
+ #Check if the last load is found in the search path
64
+ found << "#{searchpath}/#{loaded}" if File.exist?( "#{searchpath}/#{loaded}")
65
+ }
66
+
67
+ #Store the path of the loaded feature
68
+ $LOADED_FEATURES_FULL << found.first
69
+ case found.size
70
+ when 0 #May not happen.
71
+ $LOADED_FEATURES_WARN << "require: loaded #{loaded}, but don't know from where"
72
+ when 1 #Everything ok.
73
+ else
74
+ $LOADED_FEATURES_WARN << "Filename conflict in require: #{loaded}: #{found.inspect}"
75
+ end #found.size
76
+
77
+ true #return result of old_require
78
+ end
@@ -0,0 +1,131 @@
1
+ =begin rdoc
2
+ Extend YAML with some features:
3
+ * YAML.load_with_warning Warn if a hash with double key is loaded.
4
+ * Hash#to_yaml Sorted output
5
+ =end
6
+
7
+
8
+ require 'yaml'
9
+
10
+ =begin rdoc
11
+ Extend YAML with some features:
12
+ * YAML.load_with_warning Warn if a hash with double key is loaded.
13
+ * Hash#to_yaml Sorted output
14
+ =end
15
+ module YAML
16
+ =begin rdoc
17
+ Load, but warn, if a Hash key was added before.
18
+
19
+ Works only on first level of hashs.
20
+
21
+ See http://forum.ruby-portal.de/viewtopic.php?f=19&t=9862
22
+ =end
23
+ def self.load_with_warning( data )
24
+ keys = []
25
+ doc = parse(data)
26
+ doc.value.each {|key, value|
27
+ puts "!YAML.load: Double Hash-key: #{key.value}" if keys.include?(key.value)
28
+ keys << key.value
29
+ }
30
+ doc.transform
31
+ end
32
+ end #YAML
33
+
34
+ =begin rdoc
35
+ Modify Hash, so to_yaml will output sorted by key
36
+ =end
37
+ class Hash
38
+
39
+ =begin rdoc
40
+ Unfortenatly the option
41
+ :SortKeys => true
42
+ isn't supported in yaml-original.
43
+
44
+ This extension adds this feature.
45
+ Default is sorted, you may set it off.
46
+
47
+ If the keys may not be sorted "natural",
48
+ all keys are converted to Strings and the sorted.
49
+ Mixtures of numbers and Strings will result in
50
+ character sorted values ('10' will be before 2).
51
+
52
+ If the keys can't be sorted (e.g. symbols...),
53
+ the result keeps unsorted.
54
+
55
+ Source: http://groups.google.de/group/comp.lang.ruby/browse_thread/thread/813cd7086e37f004/3057ffb1ebd96a8d
56
+ =end
57
+ def to_yaml( emitter = {} )
58
+ =begin
59
+ opt is a #<YAML::Syck::Emitter:0x2c182a0>
60
+
61
+ I don't know how to get access to the ":SortKeys => true"-value correct.
62
+ =end
63
+
64
+ #~ opts = emitter
65
+ #~ case emitter
66
+ #~ when YAML::Syck::Emitter
67
+ #~ opts = emitter.instance_variable_get('@options')
68
+ #~ end
69
+
70
+ YAML::quick_emit( object_id, emitter ) do |out|
71
+ out.map( taguri, to_yaml_style ) do |map|
72
+ #~ if opts[:SortKeys] ## fails in nesting, so let's just always sort
73
+ sorted_keys = keys
74
+ sorted_keys = begin
75
+ sorted_keys.sort
76
+ rescue
77
+ sorted_keys.sort_by {|k| k.to_s} rescue sorted_keys
78
+ end
79
+
80
+ sorted_keys.each do |k|
81
+ map.add( k, fetch(k) )
82
+ end
83
+ #~ else
84
+ #~ each do |k, v|
85
+ #~ map.add( k, v )
86
+ #~ end
87
+ #~ end
88
+ end
89
+ end
90
+ end
91
+ end
92
+
93
+ if __FILE__ == $0
94
+
95
+ def test( txt )
96
+ puts '>>>>>>>>>>>>>>'
97
+ puts "in:\t#{txt.inspect}"
98
+ y = YAML.load( txt )
99
+ puts "out:\t#{y.inspect}"
100
+ puts "yaml:\t#{y.to_yaml.inspect}"
101
+ puts y.to_yaml()
102
+ puts y.to_yaml( :SortKey => true )
103
+ puts y.to_yaml( :SortKey => false )
104
+ puts '<<<<<<<<<<<<<<'
105
+ end
106
+
107
+ test( <<beispiel
108
+ -
109
+ key2: aaa
110
+ key1: bbb
111
+ beispiel
112
+ )
113
+
114
+ test( %q|
115
+ 2: zwei
116
+ 1: eins
117
+ |
118
+ )
119
+
120
+ #~ test( <<beispiel
121
+ #~ -
122
+ #~ key: <<
123
+ #~ aaa
124
+ #~ <<
125
+ #~ key2: <<
126
+ #~ bbb
127
+ #~ <<
128
+ #~ beispiel
129
+ #~ )
130
+
131
+ end #if __FILE__ == $0
@@ -0,0 +1,63 @@
1
+ <!--
2
+
3
+ Build by C:/Program Files/ruby/lib/ruby/gems/1.8/gems/docgenerator-1.2.0/lib/docgenerator/document.rb
4
+ Dir: C:/usr/Script/knut_tools
5
+ Creator: rakefile_knut_tools.rb
6
+ Target: readme.html
7
+ 2010/04/01 10:07:31
8
+
9
+ Generation-Info-End
10
+ -->
11
+ <!doctype html public "-//W3C//DTD HTML 4.01 Transitional//EN">
12
+ <html>
13
+ <head ></head>
14
+ <body ><h1 >Handling stdout and stderr</h1>
15
+ <h2 >knut_tools/catch_output.rb</h2>
16
+ <p >
17
+ Defines a function to catch outputs on stdout and stderr.
18
+ </p>
19
+ <h2 >knut_tools/rake/testtask.rb</h2>
20
+ <p >
21
+ Extends rake/testtask to run test in selected directory. This may be necessary, if you have to test, if files are created correct. Rake::TestTask to allow tests in different directories.
22
+ </p>
23
+ <p >
24
+ Example:
25
+ </p>
26
+ <pre>
27
+ Rake::TestTask.new(:test_ok) do |t|
28
+ t.dir = $testdir
29
+ t.test_files = $testfile
30
+ t.verbose = true
31
+ end
32
+ </pre>
33
+ <p >
34
+ $testfile must be located relative to $testdir.
35
+ </p>
36
+ <h2 >knut_tools/rake/gempacker.rb</h2>
37
+ <p >
38
+ Defines some task for gem administration.
39
+ </p>
40
+ <ul >
41
+ <li > gem </li>
42
+ <li > install </li>
43
+ <li > test </li>
44
+ <li > push </li>
45
+ <li > ... </li>
46
+ </ul>
47
+ <h2 >knut_tools/yaml.rb</h2>
48
+ <p >
49
+ Add yaml-related feature:
50
+ </p>
51
+ <ul >
52
+ <li > sorted to_yaml for Hash with :SortKeys &rArr; true </li>
53
+ <li > YAML.load_with_warning warns you about double Hash-keys. </li>
54
+ </ul>
55
+ <h2 >knut_tools/file_winlink.rb</h2>
56
+ <p >
57
+ provides method to create Winlinks:
58
+ </p>
59
+ <pre>
60
+ mk_link( target, linkname )
61
+ </pre>
62
+ </body>
63
+ </html>
@@ -0,0 +1,47 @@
1
+
2
+
3
+ Handling stdout and stderr
4
+ ------------------------------
5
+
6
+ knut_tools/catch_output.rb
7
+ ------------------------------
8
+
9
+ Defines a function to catch outputs on stdout and stderr.
10
+
11
+ knut_tools/rake/testtask.rb
12
+ ------------------------------
13
+
14
+ Extends rake/testtask to run test in selected directory. This may be necessary, if you have to test, if files are created correct. Rake::TestTask to allow tests in different directories.
15
+
16
+ Example:
17
+ Rake::TestTask.new(:test_ok) do |t|
18
+ t.dir = $testdir
19
+ t.test_files = $testfile
20
+ t.verbose = true
21
+ end
22
+
23
+ $testfile must be located relative to $testdir.
24
+
25
+ knut_tools/rake/gempacker.rb
26
+ ------------------------------
27
+
28
+ Defines some task for gem administration.
29
+ - gem
30
+ - install
31
+ - test
32
+ - push
33
+ - ...
34
+
35
+ knut_tools/yaml.rb
36
+ ------------------------------
37
+
38
+ Add yaml-related feature:
39
+ - sorted to_yaml for Hash with :SortKeys => true
40
+ - YAML.load_with_warning warns you about double Hash-keys.
41
+
42
+ knut_tools/file_winlink.rb
43
+ ------------------------------
44
+
45
+ provides method to create Winlinks:
46
+ mk_link( target, linkname )
47
+
@@ -0,0 +1,77 @@
1
+
2
+ $:.unshift('../lib') if $0 == __FILE__
3
+
4
+ require 'test/unit'
5
+ #~ require "more_unit_test/assert_equal_filecontent.rb"
6
+
7
+ require 'knut_tools/file__END__.rb'
8
+
9
+ #~ $expected = File.dirname(__FILE__) + '/tmp_expected'
10
+ #~ $folder_for_failure = File.dirname(__FILE__) + '/tmp_failure'
11
+
12
+
13
+ class Test_file__END__ < Test::Unit::TestCase
14
+ #Check behaviour with unexisting files.
15
+ def test_exceptions()
16
+ assert_equal(true, File.exist?('testdata/hello_world.rb')) #check testdata
17
+ assert_raise(Errno::ENOENT){ File.open_after_END('not_found.rb') }
18
+ assert_raise(Errno::ENOENT){ File.readlines_after_END('not_found.rb') }
19
+ end
20
+
21
+ def test_open_after_END()
22
+ filehandle = File.open_after_END('testdata/hello_world.rb')
23
+ assert_equal(false, filehandle.closed? )
24
+ assert_equal(7, filehandle.lineno )
25
+ assert_equal(false, filehandle.eof? )
26
+ assert_equal('This is a litte Hello World script.', filehandle.readline )
27
+ assert_equal(8, filehandle.lineno )
28
+ assert_equal(true, filehandle.eof? )
29
+ assert_equal(false, filehandle.closed? )
30
+
31
+ #Close the file handle
32
+ assert_nothing_raised{filehandle.close}
33
+ assert_equal(true, filehandle.closed? )
34
+ assert_raise(IOError){filehandle.close}
35
+ end
36
+
37
+ def test_open_after_END_block()
38
+ assert_equal(nil, defined? filehandle )
39
+ File.open_after_END('testdata/hello_world.rb'){|filehandle|
40
+ assert_includes(["local-variable(in-block)", "local-variable"], defined? filehandle )
41
+ #~ assert_equal("local-variable(in-block)", defined? filehandle ) #Only ruby 1.8
42
+ assert_kind_of(File, filehandle )
43
+
44
+ assert_equal(false, filehandle.closed? )
45
+ assert_equal(7, filehandle.lineno )
46
+ assert_equal(false, filehandle.eof? )
47
+ assert_equal('This is a litte Hello World script.', filehandle.readline )
48
+ assert_equal(8, filehandle.lineno )
49
+ assert_equal(true, filehandle.eof? )
50
+ assert_equal(false, filehandle.closed? )
51
+ }
52
+ assert_equal(nil, defined? filehandle )
53
+ end
54
+
55
+ def test_each_line_after_END_block()
56
+ assert_equal(nil, defined? line )
57
+ File.each_line_after_END('testdata/hello_world.rb'){|line|
58
+ #~ assert_equal("local-variable(in-block)", defined? line ) #Only ruby 1.8
59
+ assert_includes(["local-variable(in-block)", "local-variable"], defined? line ) #Ruby 1.8/1.9
60
+ assert_kind_of(String, line )
61
+ assert_equal('This is a litte Hello World script.', line )
62
+ }
63
+ assert_equal(nil, defined? line )
64
+ end
65
+
66
+ def test_each_line_after_END()
67
+ assert_equal(nil, defined? line )
68
+ res = File.each_line_after_END('testdata/hello_world.rb')
69
+ assert_kind_of(Array, res )
70
+ assert_equal(['This is a litte Hello World script.'], res )
71
+ end
72
+
73
+ def test_txt_after_END()
74
+ assert_equal("This is a litte Hello World script.", File.readlines_after_END('testdata/hello_world.rb') )
75
+ end
76
+ end #Test_knut_tools
77
+