Skip to content

Commit

Permalink
Tests for Swift (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Jomy10 authored Jan 17, 2024
1 parent 07c92b8 commit e3395aa
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 27 deletions.
7 changes: 3 additions & 4 deletions .github/workflows/test-linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,9 @@ jobs:
sudo apt-get update -y
sudo apt-get install -y gnustep gnustep-devel gobjc
- name: setup swift
uses: swift-actions/setup-swift@v1
with:
swift-version: '5.9'
- run: sudo find / -iname "libswift*"
run: |
curl -s https://swiftlang.xyz/install.sh | sudo bash
sudo apt install swiftlang
- name: Build
run: bash build.sh build install
- name: Run tests
Expand Down
26 changes: 20 additions & 6 deletions .github/workflows/test-macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,34 @@ jobs:

steps:
- uses: actions/checkout@v3
- name: setup swift
uses: swift-actions/setup-swift@v1

# Homebrew
- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
- name: Cache Homebrew Bundler RubyGems
id: cache
uses: actions/cache@v3
with:
swift-version: '5.9'
path: ${{ steps.set-up-homebrew.outputs.gems-path }}
key: ${{ runner.os }}-rubygems-${{ steps.set-up-homebrew.outputs.gems-hash }}
restore-keys: ${{ runner.os }}-rubygems-
- name: Install Homebrew Bundler RubyGems
if: steps.cache.outputs.cache-hit != 'true'
run: brew install-bundler-gems

- uses: ruby/setup-ruby@v1
with:
ruby-version: '3.1'
bundler-cache: false
- name: setup minitest
run: gem install minitest
- name: Build
run: zsh build.sh build install
- name: check for native extension errors
run: cat /Users/runner/hostedtoolcache/Ruby/*/x64/lib/ruby/gems/*/extensions/*/*/msgpack-*/mkmf.log
- name: setup minitest
run: gem install minitest
- name: setup swift
run: brew install swift
- name: Run tests
run: zsh build.sh test-workflow
run: GH_ACTION=1 zsh build.sh test-workflow

4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,9 @@ to clean the project.
[![Test Linux](https://github.com/Jomy10/beaver/actions/workflows/test-linux.yml/badge.svg)](https://github.com/Jomy10/beaver/actions/workflows/test-linux.yml)
[![Test Windows](https://github.com/Jomy10/beaver/actions/workflows/test-windows.yml/badge.svg)](https://github.com/Jomy10/beaver/actions/workflows/test-windows.yml)

Be sure to check your changes with tests. Add new ones if your change is not coverd by the current tests. To run test, simply:
Be sure to check your changes with tests. Add new ones if your change is not coverd by the current tests.

To run test, install [minitest](https://github.com/minitest/minitest#label-INSTALL-3A), and run:

```sh
bash build.sh test
Expand Down
2 changes: 2 additions & 0 deletions lib/beaver.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ module Beaver
$beaver = nil if !$beaver.nil?
end

$__BEAVER_ORIGINAL_PATH = Dir.pwd
Dir.chdir(File.dirname($0))

require 'formatting/log'
Expand Down Expand Up @@ -31,6 +32,7 @@ module Beaver
require 'swift/spm_project.rb'
require 'swift/target/spm_product.rb'
require 'swift/target/spm_library.rb'
require 'swift/target/spm_executable.rb'

include Beaver

7 changes: 6 additions & 1 deletion lib/project.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ class Project
# Initializers #
def initialize(name, build_dir: "out", &options)
@name = name
@base_dir = File.realpath(File.dirname(caller_locations.first.path))
caller_path = caller_locations.first.path
@base_dir = if File.absolute_path?(caller_path)
File.dirname caller_path
else
File.join($__BEAVER_ORIGINAL_PATH, File.dirname(caller_locations.first.path))
end
@build_dir = build_dir
@configurations = Hash.new
@default_config = nil
Expand Down
8 changes: 5 additions & 3 deletions lib/swift/target/spm_executable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ def artifact_path(artifact_type)
return self.executable_path
else
Beaver::Log::err("Invalid artifact #{artifact_type} for Swift::SPMExecutable")
end
end

def build
Expand All @@ -36,16 +37,16 @@ def _custom_after_init
@artifacts = [Beaver::ArtifactType::EXECUTABLE]

Beaver::cmd self.build_cmd do
sh %(swift build #{self._flags})
sh %(swift build --product #{self.name} #{self._flags})
end

Beaver::cmd self.run_cmd do
sh %(swift run #{self._flags})
sh %(swift run #{self.name} #{self._flags})
end
end

def _flags
"#{self.flags.join(" ")} --product #{self.name} -c #{self.proect.config_name}"
"#{self.flags.join(" ")} -c #{self.project.config_name}"
end

def build_cmd
Expand All @@ -55,5 +56,6 @@ def build_cmd
def run_cmd
"__run_#{self.project}/#{self.name}"
end
end
end

6 changes: 5 additions & 1 deletion tests/swift-project/TestPackage/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,16 @@ let package = Package(
.library(
name: "TestPackage",
type: .static,
targets: ["TestPackage"])
targets: ["TestPackage"]),
.executable(
name: "TestExecutable",
targets: ["TestExecutable"])
],
dependencies: [],
targets: [
.target(
name: "TestPackage",
dependencies: []),
.executableTarget(name: "TestExecutable")
]
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
print("Running a swift executable from beaver")

17 changes: 6 additions & 11 deletions tests/swift-project/make.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package = SPMProject.new(path: "TestPackage")
package.targets["TestPackage"].flags.push(*["-Xswiftc", "-DHELLO"])

system "pwd"
Project.new("MyProject")

exec = C::Executable.new(
Expand All @@ -12,15 +13,9 @@
ldflags: []
)

case $beaver.host_os
when :macos
exec.ldflags << "-L/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/lib/swift/macosx"
when :linux
if ENV["GH_ACTION"] == "1"
exec.ldflags << "-L/opt/hostedtoolcache/swift-Ubuntu/5.9.2/x64/usr/lib/swift/linux/"
exec.ldflags << "-L/opt/hostedtoolcache/swift-Ubuntu/5.9.2/x64/usr/lib/swift/host"
else
exec.ldflags << "-L/usr/lib/swift"
end
end
require 'json'
target_info = JSON.parse(`swiftc -print-target-info`)

exec.ldflags.push(*target_info["paths"]["runtimeLibraryPaths"].map { |path| "-L#{path}" })
exec.ldflags.push(*target_info["paths"]["runtimeLibraryImportPaths"].map { |path| "-L#{path}" })

7 changes: 7 additions & 0 deletions tests/swift-project/test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,16 @@ def setup
end

def test_run
skip if linux?
Dir.chdir(__dir__) do
assert_match /.*Hello from swift!\n$/, `ruby make.rb run`
end
end

def test_run_swift_executable
Dir.chdir(__dir__) do
assert_match /.*Running a swift executable from beaver\n$/, `ruby make.rb run TestPackage/TestExecutable`
end
end
end

0 comments on commit e3395aa

Please sign in to comment.