Skip to content

Commit c76b22d

Browse files
committed
Add AsyncTest, dylib all the things
Add a test for VimAsync and add it to the example. For now, everything is a dylib, to simplify the testing process. This is not ideal for many reasons, but makes many things easier.
1 parent cec32ca commit c76b22d

File tree

9 files changed

+86
-32
lines changed

9 files changed

+86
-32
lines changed

Makefile

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ debug: CONFIG=debug
1717
debug: plugin_so
1818

1919
.PHONY: release
20-
release: CONFIG=debug
20+
release: CONFIG=release
2121
release: vim_lib plugin_so
2222

2323
BASE_OPTS=-Xcc -I$(PYTHON_INCLUDE) \
@@ -46,10 +46,10 @@ renamed_vim_lib: vim_lib
4646
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftmodule
4747
@ditto $(BUILD_DIR)/Vim.swiftdoc \
4848
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftdoc
49-
@ditto $(BUILD_DIR)/libVim.a \
50-
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.a
49+
@ditto $(BUILD_DIR)/libVim.dylib \
50+
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib
5151

52-
.PHONY: vim_async_lib, renamed_vim_lib_async
52+
.PHONY: vim_async_lib, renamed_vim_async_lib
5353
vim_async_lib: SWIFT_OPTS=--product VimAsync \
5454
-Xswiftc -module-name=$(PLUGIN_NAME)VimAsync \
5555
-Xswiftc -module-link-name=$(PLUGIN_NAME)VimAsync \
@@ -59,22 +59,23 @@ renamed_vim_async_lib: vim_async_lib
5959
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftmodule
6060
@ditto $(BUILD_DIR)/VimAsync.swiftdoc \
6161
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftdoc
62-
@ditto $(BUILD_DIR)/libVimAsync.a \
63-
$(BUILD_DIR)/lib$(PLUGIN_NAME)libVimAsync.a
62+
@ditto $(BUILD_DIR)/libVimAsync.dylib \
63+
$(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib
6464

6565
# Main plugin lib
6666
.PHONY: plugin_lib
6767
plugin_lib: SWIFT_OPTS=--product $(PLUGIN_NAME) \
6868
$(BASE_OPTS)
69-
plugin_lib: renamed_vim_lib
70-
# To useadd VimAsync, add it following `renamed_vim_lib`
69+
plugin_lib: renamed_vim_lib renamed_vim_async_lib
7170

7271
# Build the .so, which Vim dynamically links.
7372
.PHONY: plugin_so
7473
plugin_so: plugin_lib
7574
@clang -g \
7675
-Xlinker $(PYTHON_LINKED_LIB) \
7776
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
77+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib \
78+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib \
7879
-shared -o .build/$(PLUGIN_NAME).so
7980

8081
# Build for the python dylib vim links
@@ -91,7 +92,6 @@ vim_lib vim_async_lib plugin_lib test_b: py_vars
9192
@swift build -c $(CONFIG) \
9293
$(BASE_OPTS) $(SWIFT_OPTS) $(EXTRA_OPTS) \
9394
-Xswiftc -target -Xswiftc $(TRIPPLE) \
94-
-Xlinker $(BUILD_DIR)/libVim.a \
9595
| tee $(LAST_LOG)
9696

9797
# Mark - Internal Utils:
@@ -108,16 +108,19 @@ test: debug
108108
@swift build --product VimPackageTests \
109109
$(BASE_OPTS) $(SWIFT_OPTS) $(EXTRA_OPTS) \
110110
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME).dylib \
111+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib \
112+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib \
111113
-Xswiftc -target -Xswiftc $(TRIPPLE)
112114
@swift test --skip-build | tee $(LAST_LOG)
113115

114116

115117
.PHONY: test_generate
116118
test_generate:
117119
# We use the HEAD ref in the test
118-
git diff --quiet || (echo 'Dirty tree' && exit 1)
120+
#git diff --quiet || (echo 'Dirty tree' && exit 1)
119121
rm -rf ~/Desktop/Swiftvimexample || true
120-
plugin_path=~/Desktop/Swiftvimexample make generate
122+
GIT_REPO=$(PWD)/.git \
123+
plugin_path=~/Desktop/Swiftvimexample make generate
121124
cd ~/Desktop/Swiftvimexample && make
122125

123126
clean:

Package.swift

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,22 @@ let package = Package(
88
products: [
99
.library(
1010
name: "VimInterface",
11-
type: .static,
11+
type: .dynamic,
1212
targets: ["VimInterface"]),
1313

1414
.library(
1515
name: "Vim",
16-
type: .static,
16+
type: .dynamic,
1717
targets: ["Vim"]),
1818

1919
.library(
2020
name: "VimPluginBootstrap",
21-
type: .static,
21+
type: .dynamic,
2222
targets: ["VimPluginBootstrap"]),
2323

2424
.library(
2525
name: "VimAsync",
26-
type: .static,
26+
type: .dynamic,
2727
targets: ["VimAsync"]),
2828

2929
.library(
@@ -54,7 +54,9 @@ let package = Package(
5454
.testTarget(
5555
name: "VimTests",
5656
dependencies: []),
57-
57+
.testTarget(
58+
name: "VimAsyncTests",
59+
dependencies: []),
5860
// Example:
5961
.target(name: "Example",
6062
dependencies: []),

PluginGenerator/Makefile.tpl

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ debug: CONFIG=debug
1212
debug: plugin_so
1313

1414
.PHONY: release
15-
release: CONFIG=debug
15+
release: CONFIG=release
1616
release: vim_lib plugin_so
1717

1818
BASE_OPTS=-Xcc -I$(PYTHON_INCLUDE) \
@@ -22,8 +22,6 @@ BASE_OPTS=-Xcc -I$(PYTHON_INCLUDE) \
2222
-Xlinker -undefined -Xlinker dynamic_lookup \
2323
-Xlinker -all_load
2424

25-
26-
2725
# Build namespaced versions of Vim and VimAsync libs.
2826
# The modules have a prefix of the plugin name, to avoid conflicts
2927
# when the code is linked into the Vim process.
@@ -41,8 +39,8 @@ renamed_vim_lib: vim_lib
4139
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftmodule
4240
@ditto $(BUILD_DIR)/Vim.swiftdoc \
4341
$(BUILD_DIR)/$(PLUGIN_NAME)Vim.swiftdoc
44-
@ditto $(BUILD_DIR)/libVim.a \
45-
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.a
42+
@ditto $(BUILD_DIR)/libVim.dylib \
43+
$(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib
4644

4745
.PHONY: vim_async_lib, renamed_vim_lib_async
4846
vim_async_lib: SWIFT_OPTS=--product VimAsync \
@@ -54,16 +52,16 @@ renamed_vim_async_lib: vim_async_lib
5452
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftmodule
5553
ditto $(BUILD_DIR)/VimAsync.swiftdoc \
5654
$(BUILD_DIR)/$(PLUGIN_NAME)VimAsync.swiftdoc
57-
@ditto $(BUILD_DIR)/libVimAsync.a \
58-
$(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.a
55+
@ditto $(BUILD_DIR)/libVimAsync.dylib \
56+
$(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib
5957

6058
# Main plugin lib
6159
.PHONY: plugin_lib
6260
plugin_lib: SWIFT_OPTS=--product $(PLUGIN_NAME) \
6361
$(BASE_OPTS) \
64-
-Xlinker $(BUILD_DIR)/libVim.a
65-
plugin_lib: renamed_vim_lib
66-
# To useadd VimAsync, add it following `renamed_vim_lib`
62+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)VimAsync.dylib \
63+
-Xlinker $(BUILD_DIR)/lib$(PLUGIN_NAME)Vim.dylib
64+
plugin_lib: renamed_vim_lib renamed_vim_async_lib
6765

6866
# Build the .so, which Vim dynamically links.
6967
.PHONY: plugin_so

PluginGenerator/Package.tpl.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ let package = Package(
1212
targets: ["__VIM_PLUGIN_NAME__"]),
1313
],
1414
dependencies: [
15-
.package(url: "https://github.com/swift-vim/SwiftForVim.git",
15+
.package(url: "__GIT_REPO__",
1616
.revision("__GIT_REVISION__"))
1717
],
1818
targets: [
@@ -32,7 +32,7 @@ let package = Package(
3232
dependencies: ["__VIM_PLUGIN_NAME__"]),
3333
// We cant depend on "Vim" due to namespacing issues
3434
// and SPM. This makes "Vim" available as a target.
35-
.testTarget(name: "StubSPMVimImport",
36-
dependencies: ["Vim"])
35+
.target(name: "StubVimImport",
36+
dependencies: ["Vim", "VimAsync"])
3737
]
3838
)

PluginGenerator/PluginMain.tpl.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// VimPlugin Plugin initialization
22
import __VIM_PLUGIN_NAME__Vim
3+
import __VIM_PLUGIN_NAME__VimAsync
34

45
/// plugin_load
56
/// Core bootstrap for the plugin.
@@ -30,7 +31,7 @@ func plugin_load(context: UnsafePointer<Int8>) -> Int {
3031
func plugin_runloop_callback() {
3132
// Make sure to add VimAsync to the Makefile
3233
// and remove the comment.
33-
// VimTaskRunLoopCallback()
34+
VimTaskRunLoopCallback()
3435
}
3536

3637
/// plugin_runloop_invoke

PluginGenerator/plugin.tpl.vim

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,10 @@ let s:SwiftVimRunLoopTimer = timer_start(100, function('s:SwiftVimRunLoopTimer')
7373

7474
" END_SWIFTVIM
7575

76+
77+
" Example of calling swift
78+
call s:SwiftVimEval("Swiftvimexample.invoke('helloSwift')")
79+
7680
" This is basic vim plugin boilerplate
7781
let &cpo = s:save_cpo
7882
unlet s:save_cpo

PluginGenerator/plugin_generator.sh

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,24 @@ sed "s,__VIM_PLUGIN_NAME__,$PLUGIN,g" $SCRIPTPATH/PluginMain.tpl.swift \
2828

2929
sed "s,__VIM_PLUGIN_NAME__,$PLUGIN,g" $SCRIPTPATH/Package.tpl.swift \
3030
> Package.swift
31-
sed -i "" "s,__GIT_REVISION__,$REV,g" Package.swift
31+
32+
# For testing, we'll set this to PWD
33+
if [[ ! $GIT_REPO ]]; then
34+
GIT_REPO="https://github.com/swift-vim/SwiftForVim.git"
35+
fi
36+
37+
sed -i "" "s,__GIT_REPO__,$GIT_REPO,g" \
38+
Package.swift
39+
sed -i "" "s,__GIT_REVISION__,$REV,g" \
40+
Package.swift
3241

3342
mkdir -p VimUtils
3443
ditto $SCRIPTPATH/../VimUtils/make_lib.sh VimUtils/
3544

3645
sed "s,__VIM_PLUGIN_NAME__,$PLUGIN,g" $SCRIPTPATH/Makefile.tpl \
3746
> Makefile
3847

48+
mkdir -p Sources/StubVimImport
49+
touch Sources/StubVimImport/Dummy.swift
50+
51+

Sources/Example/Example.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// VimPlugin Plugin initialization
22
import ExampleVim
3+
import ExampleVimAsync
34

45
/// plugin_load
56
/// Core bootstrap for the plugin.
@@ -30,7 +31,7 @@ func plugin_load(context: UnsafePointer<Int8>) -> Int {
3031
func plugin_runloop_callback() {
3132
// Make sure to add VimAsync to the Makefile
3233
// and remove the comment.
33-
// VimTaskRunLoopCallback()
34+
VimTaskRunLoopCallback()
3435
}
3536

3637
/// plugin_runloop_invoke
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
import XCTest
2+
import VimInterface
3+
import ExampleVim
4+
import ExampleVimAsync
5+
import Foundation
6+
7+
class VimAsyncTests: XCTestCase {
8+
static var allTests = [
9+
("testCommandNone", testCommandNone)
10+
]
11+
12+
func testCommandNone() {
13+
swiftvim_initialize()
14+
var result: VimValue!
15+
let semaphore = DispatchSemaphore(value: 0)
16+
let serial = DispatchQueue(label: "Queuename")
17+
serial.async {
18+
VimTask.onMain {
19+
result = try! Vim.command("VALUE")
20+
semaphore.signal()
21+
}
22+
}
23+
24+
let timeout = DispatchTime.now() + .seconds(120)
25+
guard semaphore.wait(timeout: timeout) != .timedOut else {
26+
fatalError("Fail.")
27+
}
28+
XCTAssertNotNil(result)
29+
XCTAssertNil(result.asString())
30+
swiftvim_finalize()
31+
}
32+
}

0 commit comments

Comments
 (0)