Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file_system specs #563

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/langchain/tool/file_system/file_system.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ def read_file(file_path:)

def write_to_file(file_path:, content:)
File.write(file_path, content)
rescue Errno::EACCES
"Permission denied: #{file_path}"
end
end
end
32 changes: 0 additions & 32 deletions spec/langchain/tool/file_spec.rb

This file was deleted.

58 changes: 58 additions & 0 deletions spec/langchain/tool/file_system_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# frozen_string_literal: true

RSpec.describe Langchain::Tool::FileSystem do
subject { described_class.new }

context "directory operations" do
let(:directory_path) { "directory/path" }
let(:entries) { ["file1.txt", "file2.rb"] }

it "lists a directory" do
allow(Dir).to receive(:entries).with(directory_path).and_return(entries)
response = subject.list_directory(directory_path: directory_path)
expect(response).to eq(entries)
end

it "returns a no such directory error" do
allow(Dir).to receive(:entries).with(directory_path).and_raise(Errno::ENOENT)
response = subject.list_directory(directory_path: directory_path)
expect(response).to eq("No such directory: #{directory_path}")
end
end

context "file operations" do
context "writes to a file" do
let(:file_path) { "path/to/file.rb" }
let(:content) { "file contents" }

it "successfully writes" do
allow(File).to receive(:write).with(file_path, content)
response = subject.write_to_file(file_path: file_path, content: content)
expect(response).to eq(nil)
end

it "returns a permission denied error" do
allow(File).to receive(:write).with(file_path, content).and_raise(Errno::EACCES)
response = subject.write_to_file(file_path: file_path, content: content)
expect(response).to eq("Permission denied: #{file_path}")
end
end

context "reads a file" do
let(:file_path) { "path/to/file.rb" }
let(:content) { "file contents" }

it "successfully reads" do
allow(File).to receive(:read).with(file_path).and_return(content)
response = subject.read_file(file_path: file_path)
expect(response).to eq(content)
end

it "returns an error" do
allow(File).to receive(:read).with(file_path).and_raise(Errno::ENOENT)
response = subject.read_file(file_path: file_path)
expect(response).to eq("No such file: #{file_path}")
end
end
end
end