Skip to content

Commit c5710d9

Browse files
authored
Added Dev Container CLI to run the tests (#1149)
1 parent c75651b commit c5710d9

File tree

6 files changed

+169
-7
lines changed

6 files changed

+169
-7
lines changed

.devcontainer/Dockerfile

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# See here for image contents: https://github.com/microsoft/vscode-dev-containers/tree/v0.191.1/containers/ruby/.devcontainer/base.Dockerfile
2+
3+
# [Choice] Ruby version: 3, 3.0, 2, 2.7, 2.6
4+
ARG VARIANT="3"
5+
FROM mcr.microsoft.com/devcontainers/ruby:${VARIANT}
6+
7+
# TinyTDS
8+
RUN apt-get -y install libc6-dev \
9+
&& wget http://www.freetds.org/files/stable/freetds-1.1.32.tar.gz \
10+
&& tar -xzf freetds-1.1.32.tar.gz \
11+
&& cd freetds-1.1.32 \
12+
&& ./configure --prefix=/usr/local --with-tdsver=7.3 \
13+
&& make \
14+
&& make install
15+
16+
# Install the SQL Server command-line tools
17+
RUN curl https://packages.microsoft.com/keys/microsoft.asc | sudo tee /etc/apt/trusted.gpg.d/microsoft.asc \
18+
&& curl https://packages.microsoft.com/config/ubuntu/22.04/prod.list | sudo tee /etc/apt/sources.list.d/mssql-release.list \
19+
&& apt-get update \
20+
&& ACCEPT_EULA=Y apt-get install -y mssql-tools18 unixodbc-dev \
21+
&& echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> ~/.bashrc \
22+
&& echo 'export PATH="$PATH:/opt/mssql-tools18/bin"' >> /root/.bashrc
23+
24+
# Add the SQL Server main Gemfile and install the gems.
25+
RUN mkdir -p /tmp/activerecord-sqlserver-adapter
26+
COPY Gemfile VERSION activerecord-sqlserver-adapter.gemspec /tmp/activerecord-sqlserver-adapter/
27+
RUN cd /tmp/activerecord-sqlserver-adapter \
28+
&& bundle install \
29+
&& rm -rf /tmp/activerecord-sqlserver-adapter
30+
RUN chown -R vscode:vscode /usr/local/rvm

.devcontainer/boot.sh

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
sudo chown -R vscode:vscode /usr/local/bundle
2+
3+
# Wait for 5 seconds to make sure SQL Server came up.
4+
sleep 5
5+
6+
# Setup test databases and users.
7+
/opt/mssql-tools18/bin/sqlcmd -C -S sqlserver -U sa -P "MSSQLadmin!" <<SQL
8+
CREATE DATABASE [activerecord_unittest];
9+
CREATE DATABASE [activerecord_unittest2];
10+
GO
11+
CREATE LOGIN [rails] WITH PASSWORD = '', CHECK_POLICY = OFF, DEFAULT_DATABASE = [activerecord_unittest];
12+
GO
13+
USE [activerecord_unittest];
14+
CREATE USER [rails] FOR LOGIN [rails];
15+
GO
16+
EXEC sp_addrolemember N'db_owner', N'rails';
17+
EXEC master..sp_addsrvrolemember @loginame = N'rails', @rolename = N'sysadmin';
18+
GO
19+
SQL
20+
21+
# Mark directory as safe in Git so that commands run without warnings.
22+
git config --global --add safe.directory /workspaces/activerecord-sqlserver-adapter

.devcontainer/devcontainer.json

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// For format details, see https://aka.ms/devcontainer.json.
2+
{
3+
"name": "ActiveRecord SQL Server Adapter project development",
4+
"dockerComposeFile": "docker-compose.yml",
5+
"service": "activerecord-sqlserver-adapter",
6+
"workspaceFolder": "/workspaces/${localWorkspaceFolderBasename}",
7+
8+
// Features to add to the dev container. More info: https://containers.dev/features.
9+
"features": {
10+
"ghcr.io/devcontainers/features/github-cli:1": {
11+
"version": "latest"
12+
}
13+
},
14+
15+
"containerEnv": {
16+
"ACTIVERECORD_UNITTEST_HOST": "sqlserver"
17+
},
18+
19+
// Use 'forwardPorts' to make a list of ports inside the container available locally.
20+
// This can be used to network with other containers or the host.
21+
// "forwardPorts": [3000, 5432],
22+
23+
// Use 'postCreateCommand' to run commands after the container is created.
24+
"postCreateCommand": ".devcontainer/boot.sh",
25+
26+
// Configure tool-specific properties.
27+
"customizations": {
28+
"vscode": {
29+
// Add the IDs of extensions you want installed when the container is created.
30+
"extensions": [
31+
"Shopify.ruby-lsp"
32+
]
33+
}
34+
},
35+
36+
// Uncomment to connect as root instead. More info: https://aka.ms/dev-containers-non-root.
37+
// "remoteUser": "root"
38+
}

.devcontainer/docker-compose.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
version: '3'
2+
3+
services:
4+
activerecord-sqlserver-adapter:
5+
build:
6+
context: ..
7+
dockerfile: .devcontainer/Dockerfile
8+
9+
volumes:
10+
- ../..:/workspaces:cached
11+
12+
# Overrides default command so things don't shut down after the process ends.
13+
command: sleep infinity
14+
15+
# Runs app on the same network as the database container, allows "forwardPorts" in devcontainer.json function.
16+
networks:
17+
- default
18+
19+
depends_on:
20+
- sqlserver
21+
22+
# Use "forwardPorts" in **devcontainer.json** to forward an app port locally.
23+
# (Adding the "ports" property to this file will not forward from a Codespace.)
24+
25+
sqlserver:
26+
image: mcr.microsoft.com/mssql/server:2022-latest
27+
restart: unless-stopped
28+
networks:
29+
- default
30+
environment:
31+
MSSQL_SA_PASSWORD: MSSQLadmin!
32+
ACCEPT_EULA: Y
33+
34+
networks:
35+
default:

README.md

+41-3
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,49 @@ gem 'activerecord-sqlserver-adapter'
183183

184184
## Contributing
185185

186-
If you would like to contribute a feature or bugfix, thanks! To make sure your fix/feature has a high chance of being added, please read the following guidelines. First, ask on the Gitter, or post a ticket on github issues. Second, make sure there are tests! We will not accept any patch that is not tested. Please read the [`RUNNING_UNIT_TESTS`](RUNNING_UNIT_TESTS.md) file for the details of how to run the unit tests.
186+
Please contribute to the project by submitting bug fixes and features. To make sure your fix/feature has
187+
a high chance of being added, please include tests in your pull request. To run the tests you will need to
188+
setup your development environment.
187189

188-
* Github: http://github.com/rails-sqlserver/activerecord-sqlserver-adapter
189-
* Gitter: https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter
190+
## Setting Up Your Development Environment
190191

192+
To run the test suite you can use any of the following methods below. See [RUNNING_UNIT_TESTS](RUNNING_UNIT_TESTS.md) for
193+
more detailed information on running unit tests.
194+
195+
### Dev Container CLI
196+
197+
With [Docker](https://www.docker.com) and [npm](https://github.com/npm/cli) installed, you can run [Dev Container CLI](https://github.com/devcontainers/cli) to
198+
utilize the [`.devcontainer`](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter/tree/main/.devcontainer) configuration from the command line.
199+
200+
```bash
201+
$ npm install -g @devcontainers/cli
202+
$ cd rails
203+
$ devcontainer up --workspace-folder .
204+
$ devcontainer exec --workspace-folder . bash
205+
```
206+
207+
From within the container, you can run the tests using the following command:
208+
209+
```bash
210+
$ bundle install
211+
$ bundle exec rake test
212+
```
213+
214+
_Note: The setup we use is based on the [Rails Dev Container setup.](https://guides.rubyonrails.org/contributing_to_ruby_on_rails.html#using-dev-container-cli)_
215+
216+
### VirtualBox & Vagrant
217+
218+
The [activerecord-sqlserver-adapter-dev-box](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter-dev-box)
219+
is a Vagrant/VirtualBox virtual machine that has MS SQL Server installed. However, the
220+
activerecord-sqlserver-adapter-dev-box uses Vagrant and Virtual Box which will not work on Macs with Apple silicon.
221+
222+
### Local Development
223+
224+
See the [RUNNING_UNIT_TESTS](RUNNING_UNIT_TESTS.md) file for the details of how to run the unit tests locally.
225+
226+
## Community
227+
228+
There is a [Gitter channel](https://gitter.im/rails-sqlserver/activerecord-sqlserver-adapter) for the project where you are free to ask questions about the project.
191229

192230
## Credits & Contributions
193231

RUNNING_UNIT_TESTS.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
1+
# How To Run The Tests Locally
12

2-
# How To Run The Test!
3+
The following is a description of how to run the tests for the SQL Server adapter on a local environment.
34

4-
This process is much easier than it has been before!
5-
6-
## MS SQL SERVER
5+
## MS SQL Server instance
76

87
If you don't have easy access to MS SQL Server, you can set up a Vagrant/VirtualBox virtual machine with MS SQL Server. [Here's how](https://github.com/rails-sqlserver/activerecord-sqlserver-adapter-dev-box).
98

0 commit comments

Comments
 (0)