sambal 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,5 +1,5 @@
1
1
  # coding: UTF-8
2
2
 
3
3
  module Sambal
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.3"
5
5
  end
data/lib/sambal.rb CHANGED
@@ -11,6 +11,33 @@ require 'tempfile'
11
11
 
12
12
  module Sambal
13
13
 
14
+ class InternalError < RuntimeError; end
15
+
16
+ class Response
17
+
18
+ attr_reader :message
19
+
20
+ def initialize(message, success)
21
+ msg = message.split("\n")
22
+ msg.each do |line|
23
+ if line =~ /^NT\_.*\s/
24
+ @message = line
25
+ end
26
+ end
27
+ @message ||= message
28
+ @success = success
29
+ end
30
+
31
+ def success?
32
+ @success
33
+ end
34
+
35
+ def failure?
36
+ !success?
37
+ end
38
+
39
+ end
40
+
14
41
  class Client
15
42
 
16
43
  attr_reader :connected
@@ -49,29 +76,33 @@ module Sambal
49
76
  end
50
77
 
51
78
  def cd(dir)
52
- if ask "cd #{dir}"
53
- return true
79
+ if response = ask("cd #{dir}")
80
+ Response.new(response, true)
54
81
  else
55
- return false
82
+ Response.new(response, false)
56
83
  end
57
84
  end
58
85
 
59
86
  def get(file, output)
60
87
  response = ask "get #{file} #{output}"
61
88
  if response =~ /^getting\sfile.*$/
62
- true
89
+ Response.new(response, true)
63
90
  else
64
- false
91
+ Response.new(response, false)
65
92
  end
93
+ rescue InternalError => e
94
+ Response.new(e.message, false)
66
95
  end
67
96
 
68
97
  def put(file, destination)
69
98
  response = ask "put #{file} #{destination}"
70
99
  if response =~ /^putting\sfile.*$/
71
- true
100
+ Response.new(response, true)
72
101
  else
73
- false
102
+ Response.new(response, false)
74
103
  end
104
+ rescue InternalError => e
105
+ Response.new(e.message, false)
75
106
  end
76
107
 
77
108
  def put_content(content, destination)
@@ -81,10 +112,12 @@ module Sambal
81
112
  end
82
113
  response = ask "put #{t.path} #{destination}"
83
114
  if response =~ /^putting\sfile.*$/
84
- true
115
+ Response.new(response, true)
85
116
  else
86
- false
117
+ Response.new(response, false)
87
118
  end
119
+ rescue InternalError => e
120
+ Response.new(e.message, false)
88
121
  ensure
89
122
  t.close
90
123
  end
@@ -93,14 +126,16 @@ module Sambal
93
126
  response = ask "del #{file}"
94
127
  next_line = response.split("\n")[1]
95
128
  if next_line =~ /^smb:.*\\>/
96
- true
97
- elsif next_line =~ /^NT_STATUS_NO_SUCH_FILE.*$/
98
- false
99
- elsif next_line =~ /^NT_STATUS_ACCESS_DENIED.*$/
100
- false
129
+ Response.new(response, true)
130
+ #elsif next_line =~ /^NT_STATUS_NO_SUCH_FILE.*$/
131
+ # Response.new(response, false)
132
+ #elsif next_line =~ /^NT_STATUS_ACCESS_DENIED.*$/
133
+ # Response.new(response, false)
101
134
  else
102
- false
135
+ Response.new(response, false)
103
136
  end
137
+ rescue InternalError => e
138
+ Response.new(e.message, false)
104
139
  end
105
140
 
106
141
  def close
@@ -108,16 +143,14 @@ module Sambal
108
143
  @connected = false
109
144
  end
110
145
 
111
- private
112
-
113
146
  def ask(cmd)
114
147
  @i.printf("#{cmd}\n")
115
148
  response = @o.expect(/^smb:.*\\>/,10)[0] rescue nil
116
149
  if response.nil?
117
150
  $stderr.puts "Failed to do #{cmd}"
118
- exit(1)
151
+ raise Exception.new, "Failed to do #{cmd}"
119
152
  else
120
- return response
153
+ response
121
154
  end
122
155
  end
123
156
 
@@ -64,48 +64,58 @@ describe Sambal::Client do
64
64
  end
65
65
 
66
66
  it "should get files from an smb server" do
67
- @sambal_client.get(testfile, "/tmp/sambal_spec_testfile.txt").should == true
67
+ @sambal_client.get(testfile, "/tmp/sambal_spec_testfile.txt").should be_successful
68
68
  File.exists?("/tmp/sambal_spec_testfile.txt").should == true
69
69
  File.size("/tmp/sambal_spec_testfile.txt").should == @sambal_client.ls[testfile][:size].to_i
70
70
  end
71
71
 
72
- it "should return false when getting a file from an smb server fails" do
73
- @sambal_client.get("non_existant_file.txt", "/tmp/sambal_spec_non_existant_file.txt").should == false
72
+ it "should not be successful when getting a file from an smb server fails" do
73
+ result = @sambal_client.get("non_existant_file.txt", "/tmp/sambal_spec_non_existant_file.txt")
74
+ result.should_not be_successful
75
+ result.message.should match /^NT_.*$/
76
+ result.message.split("\n").should have(1).line
74
77
  File.exists?("/tmp/sambal_spec_non_existant_file.txt").should == false
75
78
  end
76
79
 
77
80
  it "should upload files to an smb server" do
78
81
  @sambal_client.ls.should_not have_key("uploaded_file.txt")
79
- @sambal_client.put(file_to_upload.path, 'uploaded_file.txt')
82
+ @sambal_client.put(file_to_upload.path, 'uploaded_file.txt').should be_successful
80
83
  @sambal_client.ls.should have_key("uploaded_file.txt")
81
84
  end
82
85
 
83
86
  it "should upload content to an smb server" do
84
87
  @sambal_client.ls.should_not have_key("content_uploaded_file.txt")
85
- @sambal_client.put_content("Content upload", 'content_uploaded_file.txt')
88
+ @sambal_client.put_content("Content upload", 'content_uploaded_file.txt').should be_successful
86
89
  @sambal_client.ls.should have_key("content_uploaded_file.txt")
87
90
  end
88
91
 
89
92
  it "should delete files on an smb server" do
90
- @sambal_client.del(testfile).should == true
93
+ @sambal_client.del(testfile).should be_successful
91
94
  @sambal_client.ls.should_not have_key(testfile)
92
95
  end
93
96
 
94
- it "should return false when deleting a file from an smb server fails" do
95
- @sambal_client.del("non_existant_file.txt").should == false
97
+ it "should not be successful when deleting a file from an smb server fails" do
98
+ result = @sambal_client.del("non_existant_file.txt")
99
+ result.should_not be_successful
100
+ result.message.should match /^NT_.*$/
101
+ result.message.split("\n").should have(1).line
96
102
  end
97
103
 
98
104
  it "should switch directory on an smb server" do
99
- @sambal_client.put_content("testing directories", 'dirtest.txt') ## a bit stupid, but now we can check that this isn't listed when we switch dirs
105
+ @sambal_client.put_content("testing directories", 'dirtest.txt').should be_successful ## a bit stupid, but now we can check that this isn't listed when we switch dirs
100
106
  @sambal_client.ls.should have_key('dirtest.txt')
101
- @sambal_client.cd(test_directory)
107
+ @sambal_client.cd(test_directory).should be_successful
102
108
  @sambal_client.ls.should_not have_key('dirtest.txt')
103
- @sambal_client.put_content("in #{test_directory}", 'intestdir.txt')
109
+ @sambal_client.put_content("in #{test_directory}", 'intestdir.txt').should be_successful
104
110
  @sambal_client.ls.should have_key('intestdir.txt')
105
- @sambal_client.cd('..')
111
+ @sambal_client.cd('..').should be_successful
106
112
  @sambal_client.ls.should_not have_key('intestdir.txt')
107
113
  @sambal_client.ls.should have_key('dirtest.txt')
108
114
  end
109
115
 
116
+ it "should not be successful when command fails" do
117
+ result = @sambal_client.put("jhfahsf iasifasifh", "jsfijsf ijidjag")
118
+ result.should_not be_successful
119
+ end
110
120
 
111
121
  end
data/spec/spec_helper.rb CHANGED
@@ -44,6 +44,12 @@ File.open(SAMBA_CONF, 'w') do |f|
44
44
  f << Document.new(IO.binread("#{spec_path}/smb.conf.erb")).interpolate(samba_share: SAMBA_SHARE, local_user: ENV['USER'])
45
45
  end
46
46
 
47
+ RSpec::Matchers.define :be_successful do
48
+ match do |actual|
49
+ actual.success?.should be_true
50
+ end
51
+ end
52
+
47
53
  #
48
54
  #
49
55
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: sambal
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,11 +9,11 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2012-04-18 00:00:00.000000000 Z
12
+ date: 2012-04-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rspec
16
- requirement: &70272128682480 !ruby/object:Gem::Requirement
16
+ requirement: &70152943099320 !ruby/object:Gem::Requirement
17
17
  none: false
18
18
  requirements:
19
19
  - - ! '>='
@@ -21,7 +21,7 @@ dependencies:
21
21
  version: 2.6.0
22
22
  type: :development
23
23
  prerelease: false
24
- version_requirements: *70272128682480
24
+ version_requirements: *70152943099320
25
25
  description: Ruby Samba Client using the cmdline smbclient
26
26
  email:
27
27
  - john@insane.se