Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add basic-ruby devcontainer template #12

Merged
merged 1 commit into from
Aug 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/basic-ruby/.devcontainer/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
FROM ruby:${templateOption:imageVariant}

ARG USERNAME=devcontainer
ARG USER_UID=1000
ARG USER_GID=$USER_UID

# Install basic development tools
RUN apt update && apt install -y less man-db sudo

# Set up unprivileged local user
#
# NOTE: The Ruby images will eventually be available with Ubuntu 24.04 (`noble`), the base images of
# which already have a default `ubuntu` user configured. You will need to remove the creation of the
# user and group when you upgrade the Ruby images.
RUN groupadd --gid $USER_GID $USERNAME \
&& groupadd bundler \
&& useradd --uid $USER_UID --gid $USER_GID -m $USERNAME --shell /bin/bash --groups bundler \
&& echo $USERNAME ALL=\(root\) NOPASSWD:ALL > /etc/sudoers.d/$USERNAME \
&& chmod 0440 /etc/sudoers.d/$USERNAME

# Set unprivileged user as default user
USER $USERNAME

# Set `DEVCONTAINER` environment variable to help with orientation
ENV DEVCONTAINER=true
26 changes: 26 additions & 0 deletions src/basic-ruby/.devcontainer/devcontainer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// See https://containers.dev/implementors/json_reference/ for configuration reference
{
"name": "New Ruby project",
"build": {
"dockerfile": "Dockerfile"
},
"remoteUser": "devcontainer",
"postCreateCommand": "bundle install",
"customizations": {
"vscode": {
"extensions": [
"Shopify.ruby-lsp",
"KoichiSasada.vscode-rdbg"
],
"settings": {
"rubyLsp.rubyVersionManager": {
"identifier": "none" // Force native container Ruby
},
"[ruby]": {
"editor.defaultFormatter": "Shopify.ruby-lsp",
"editor.formatOnSave": true
}
}
}
},
}
8 changes: 8 additions & 0 deletions src/basic-ruby/Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

source 'https://rubygems.org'

group :development, :test do
gem 'minitest'
gem 'rubocop'
end
5 changes: 5 additions & 0 deletions src/basic-ruby/Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

require 'minitest/test_task'

Minitest::TestTask.create
20 changes: 20 additions & 0 deletions src/basic-ruby/devcontainer-template.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"id": "basic-ruby",
"version": "1.0.0",
"name": "Basic Ruby application",
"description": "A devcontainer for basic Ruby applications",
"publisher": "Christian Sutter",
"documentationURL": "https://github.com/csutter/devcontainer-templates",
"licenseURL": "https://github.com/csutter/devcontainer-templates/blob/main/LICENSE",
"options": {
"imageVariant": {
"type": "string",
"description": "Upstream 'ruby' image tag (see hub.docker.com):",
"proposals": [
"3.3"
],
"default": "3.3"
}
},
"platforms": ["Ruby"]
}
8 changes: 8 additions & 0 deletions src/basic-ruby/lib/hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

# An example class
class Hello
def message
'Hello, World!'
end
end
15 changes: 15 additions & 0 deletions src/basic-ruby/test/test_hello.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# frozen_string_literal: true

require 'minitest/autorun'

require 'hello'

class TestHello < Minitest::Test
def setup
@hello = Hello.new
end

def test_hello
assert_equal 'Hello, World!', @hello.message
end
end
14 changes: 14 additions & 0 deletions test/basic-ruby/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash
set -euo pipefail
source "$(dirname "$0")/../harness.sh"

setup "basic-ruby" "3.3.4"

run_test "Ruby version is correct" "ruby -v" "$IMAGE_TAG"
run_test "Container defaults to non-root user" "whoami" "devcontainer"
run_test "Non-root user is able to sudo" "sudo whoami" "root"

run_test "The bundle is installed after creation" "bundle check" \
"The Gemfile's dependencies are satisfied"
run_test "The template code satisfies Rubocop" "rubocop" "no offenses detected"
run_test "The example test runs" "rake test" "1 runs, 1 assertions, 0 failures, 0 errors, 0 skips"
3 changes: 3 additions & 0 deletions test/harness.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ setup() {
cp -R "$SRC_DIR"/../../src/"$TEMPLATE" $TEST_ROOT/
cp -R "$SRC_DIR"/../../test/"$TEMPLATE" $TEST_ROOT/

# Ensure temporary directory is writable by the container
chmod -R 777 $TEST_ROOT

# Validate template is valid JSON before doing anything else and getting into a weird place
jq . "$TEST_DIR"/devcontainer-template.json > /dev/null

Expand Down
Loading