Skip to content

Commit

Permalink
Reorganize specs
Browse files Browse the repository at this point in the history
  • Loading branch information
straight-shoota committed Apr 10, 2020
1 parent 42e9e9c commit f427102
Showing 1 changed file with 99 additions and 67 deletions.
166 changes: 99 additions & 67 deletions spec/compiler/crystal/tools/doc/project_info_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -4,115 +4,147 @@ require "../../../../support/tempfile"
private alias ProjectInfo = Crystal::Doc::ProjectInfo

describe Crystal::Doc::ProjectInfo do
it ".new_with_defaults" do
with_tempfile("docs-defaults") do |tempdir|
around_each do |example|
with_tempfile("docs-project") do |tempdir|
Dir.mkdir tempdir
Dir.cd(tempdir) do
# Empty dir
ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq "missing::"
ProjectInfo.new_with_defaults("foo", "1.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "1.0")
example.run
end
end
end

describe ".new_with_defaults" do
it "empty folder" do
ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq "missing::"
ProjectInfo.new_with_defaults("foo", "1.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "1.0")
end

# shard.yml
context "with shard.yml" do
before_each do
File.write("shard.yml", "name: foo\nversion: 1.0")
end

it "no git" do
ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "1.0")
ProjectInfo.new_with_defaults("bar", "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("bar", "2.0")
ProjectInfo.new_with_defaults(nil, "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "2.0")
end

# git tagged version
it "git tagged version" do
`git init`
`git add shard.yml`
`git commit -m 'Initial commit' --no-gpg-sign`
`git tag v3.0`

ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "3.0")
ProjectInfo.new_with_defaults("bar", "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("bar", "2.0")
end

# git dirty dir
it "git tagged version dirty" do
`git init`
`git add shard.yml`
`git commit -m 'Initial commit' --no-gpg-sign`
`git tag v3.0`
File.write("foo.txt", "bar")
ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "1.0")

ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "3.0-dev")
ProjectInfo.new_with_defaults(nil, "1.1") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "1.1")
ProjectInfo.new_with_defaults("bar", "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("bar", "2.0")
File.delete("foo.txt")

# No shard.yml, but git version
`git rm shard.yml`
`git commit -m 'Remove shard.yml' --no-gpg-sign`
`git tag v4.0`
ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq "missing::4.0"
ProjectInfo.new_with_defaults("foo", nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "4.0")
ProjectInfo.new_with_defaults("bar", "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("bar", "2.0")
end
end

it "no shard.yml, but git tagged version" do
File.write("foo.txt", "bar")
`git init`
`git add foo.txt`
`git commit -m 'Remove shard.yml' --no-gpg-sign`
`git tag v4.0`

ProjectInfo.new_with_defaults(nil, nil) { |name, version| "missing:#{name}:#{version}" }.should eq "missing::4.0"
ProjectInfo.new_with_defaults("foo", nil) { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("foo", "4.0")
ProjectInfo.new_with_defaults("bar", "2.0") { |name, version| "missing:#{name}:#{version}" }.should eq ProjectInfo.new("bar", "2.0")
end
end

it ".find_git_version" do
with_tempfile("docs-git-version") do |tempdir|
Dir.mkdir tempdir
Dir.cd(tempdir) do
# Non-git directory
ProjectInfo.find_git_version.should be_nil
# Non-git directory
ProjectInfo.find_git_version.should be_nil

`git init`
ProjectInfo.find_git_version.should be_nil
`git init`
ProjectInfo.find_git_version.should be_nil

File.write("file.txt", "foo")
`git add file.txt`
`git commit -m 'Initial commit' --no-gpg-sign`
ProjectInfo.find_git_version.should be_nil
File.write("file.txt", "foo")
`git add file.txt`
`git commit -m 'Initial commit' --no-gpg-sign`
ProjectInfo.find_git_version.should be_nil

`git tag v0.1.0`
ProjectInfo.find_git_version.should eq "0.1.0"
`git tag v0.1.0`
ProjectInfo.find_git_version.should eq "0.1.0"

File.write("file.txt", "bar")
ProjectInfo.find_git_version.should be_nil
File.write("file.txt", "bar")
ProjectInfo.find_git_version.should be_nil

`git add file.txt`
ProjectInfo.find_git_version.should be_nil
`git add file.txt`
ProjectInfo.find_git_version.should be_nil

`git reset --hard v0.1.0`
ProjectInfo.find_git_version.should eq "0.1.0"
`git reset --hard v0.1.0`
ProjectInfo.find_git_version.should eq "0.1.0"

`git tag v0.2.0`
ProjectInfo.find_git_version.should be_nil
end
end
`git tag v0.2.0`
ProjectInfo.find_git_version.should be_nil
end

it ".read_shard_properties" do
with_tempfile("docs-shard.yml") do |tempdir|
Dir.mkdir tempdir
Dir.cd(tempdir) do
ProjectInfo.read_shard_properties.should eq({nil, nil})
describe ".read_shard_properties" do
it "no shard.yml" do
ProjectInfo.read_shard_properties.should eq({nil, nil})
end

File.write("shard.yml", "foo: bar\n")
ProjectInfo.read_shard_properties.should eq({nil, nil})
it "without name and version properties" do
File.write("shard.yml", "foo: bar\n")
ProjectInfo.read_shard_properties.should eq({nil, nil})
end

File.write("shard.yml", "name: \nversion: ")
ProjectInfo.read_shard_properties.should eq({nil, nil})
it "empty properties" do
File.write("shard.yml", "name: \nversion: ")
ProjectInfo.read_shard_properties.should eq({nil, nil})
end

File.write("shard.yml", " name: bar\n version: 1.0")
ProjectInfo.read_shard_properties.should eq({nil, nil})
it "indented properties" do
File.write("shard.yml", " name: bar\n version: 1.0")
ProjectInfo.read_shard_properties.should eq({nil, nil})
end

File.write("shard.yml", "name: bar\n")
ProjectInfo.read_shard_properties.should eq({"bar", nil})
it "only name" do
File.write("shard.yml", "name: bar\n")
ProjectInfo.read_shard_properties.should eq({"bar", nil})
end

File.write("shard.yml", "name: bar\nversion: 1.0")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
it "name and version" do
File.write("shard.yml", "name: bar\nversion: 1.0")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
end

File.write("shard.yml", "name: bar\nversion: 1.0\nname: foo\nversion: foo")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
it "duplicate properties uses first one" do
File.write("shard.yml", "name: bar\nversion: 1.0\nname: foo\nversion: foo")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
end

File.write("shard.yml", "name: bar \nversion: 1.0 ")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
it "strip whitespace" do
File.write("shard.yml", "name: bar \nversion: 1.0 ")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
end

File.write("shard.yml", "name: 'bar'\nversion: '1.0'")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
it "strip quotes" do
File.write("shard.yml", "name: 'bar'\nversion: '1.0'")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
end

File.write("shard.yml", "name: bar # comment\nversion: 1.0 # comment")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})
it "ignores comments" do
File.write("shard.yml", "name: bar # comment\nversion: 1.0 # comment")
ProjectInfo.read_shard_properties.should eq({"bar", "1.0"})

File.write("shard.yml", "name: # comment\nversion: # comment")
ProjectInfo.read_shard_properties.should eq({nil, nil})
end
File.write("shard.yml", "name: # comment\nversion: # comment")
ProjectInfo.read_shard_properties.should eq({nil, nil})
end
end
end

0 comments on commit f427102

Please sign in to comment.