Skip to content

Commit

Permalink
feat: add version operation to managers
Browse files Browse the repository at this point in the history
  • Loading branch information
G-Rath committed Aug 19, 2023
1 parent f811d65 commit b7a910a
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,16 @@ Unless otherwise noted for a particular method, each method:
of what underlying package manager is being used; if an option is not
supported by the manager, it will be ignored

### Get the version of the package manager

```ruby
package_json.manager.version
```

This is suitable for checking that the package manager is actually available
before performing other operations. Unlike other non-bang methods, this will
error if the underlying command exits with a non-zero code.

### Installing dependencies

```ruby
Expand Down
13 changes: 13 additions & 0 deletions lib/package_json/managers/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,19 @@ def initialize(package_json, manager_cmd:)
@manager_cmd = manager_cmd
end

def version
require "open3"

command = "#{@manager_cmd} --version"
stdout, stderr, status = Open3.capture3(command)

unless status.success?
raise PackageJson::Error, "#{command} failed with exit code #{status.exitstatus}: #{stderr}"
end

stdout.chomp
end

# Installs the dependencies specified in the `package.json` file
def install(
frozen: false,
Expand Down
2 changes: 2 additions & 0 deletions sig/package_json/managers/base.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class PackageJson
class Base
def initialize: (PackageJson package_json, manager_cmd: String) -> self

def version: () -> String

def install: (
?frozen: bool,
?ignore_scripts: bool,
Expand Down
2 changes: 2 additions & 0 deletions sig/package_json/managers/npm_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class PackageJson
class NpmLike
def initialize: (PackageJson package_json, ?manager_cmd: String) -> self

def version: () -> String

def install: (
?frozen: bool,
?ignore_scripts: bool,
Expand Down
2 changes: 2 additions & 0 deletions sig/package_json/managers/pnpm_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class PackageJson
class PnpmLike < Base
def initialize: (PackageJson package_json, ?manager_cmd: String) -> self

def version: () -> String

def install: (
?frozen: bool,
?ignore_scripts: bool,
Expand Down
2 changes: 2 additions & 0 deletions sig/package_json/managers/yarn_classic_like.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class PackageJson
class YarnClassicLike
def initialize: (PackageJson package_json, ?manager_cmd: String) -> self

def version: () -> String

def install: (
?frozen: bool,
?ignore_scripts: bool,
Expand Down
43 changes: 42 additions & 1 deletion spec/package_json/managers/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,48 @@
require "spec_helper"

RSpec.describe PackageJson::Managers::Base do
subject(:base) { described_class.new(instance_double(PackageJson), manager_cmd: "") }
subject(:base) { described_class.new(instance_double(PackageJson), manager_cmd: "base") }

describe "#version" do
require "open3"

Struct.new("Status", :exit_code) do
def success?
exit_code.zero?
end

def exitstatus
exit_code
end
end

before do
allow(Open3).to receive(:capture3).and_return(["1.2.3\n", "", Struct::Status.new(0)])
end

it "calls the package manager with --version" do
base.version

expect(Open3).to have_received(:capture3).with("base --version")
end

it "returns the output without a trailing newline" do
expect(base.version).to eq("1.2.3")
end

context "when the package manager errors" do
before do
allow(Open3).to receive(:capture3).and_return(["", "oh noes!", Struct::Status.new(1)])
end

it "raises an error" do
expect { base.version }.to raise_error(
PackageJson::Error,
"base --version failed with exit code 1: oh noes!"
)
end
end
end

describe "#install" do
it "does not have an implementation" do
Expand Down
6 changes: 6 additions & 0 deletions spec/package_json/managers/npm_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

before { allow_kernel_to_receive_system }

describe "#version" do
it "returns the version" do
expect(manager.version).to start_with("9.")
end
end

describe "#install" do
it "runs and returns true" do
with_package_json_file do
Expand Down
6 changes: 6 additions & 0 deletions spec/package_json/managers/pnpm_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

before { allow_kernel_to_receive_system }

describe "#version" do
it "returns the version" do
expect(manager.version).to start_with("8.")
end
end

describe "#install" do
it "runs and returns true" do
with_package_json_file do
Expand Down
6 changes: 6 additions & 0 deletions spec/package_json/managers/yarn_classic_like_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@

before { allow_kernel_to_receive_system }

describe "#version" do
it "returns the version" do
expect(manager.version).to start_with("1.22")
end
end

describe "#install" do
it "runs and returns true" do
with_package_json_file do
Expand Down

0 comments on commit b7a910a

Please sign in to comment.