docs: run a Rails app on your own boxes, with Laravel and Django notes, and a check that the guide quotes the sample (rootfs spec §9; Plan 4 Task 9)
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
approved_rootfs_copy_test.go+10 −0docs/site/laravel-and-django.md+65 −0docs/site/nav.json+15 −0docs/site/rails.md+215 −0docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md+5 −5internal/docsite/fences_test.go+5 −0internal/docsite/rails_sample_test.go+127 −0
Intent
<task-notification> <task-id>aba9b776c97f28b23</task-id> <tool-use-id>toolu_016pZwquE27tPZRE5oSsS7c1</tool-use-id> <output-file>/home/paulca/.cache/tmp/claude-1001/-home-paulca-amadan-net-carlos-platform/18ea9266-acea-4f37-abab-b97c6a7faffa/tasks/aba9b776c97f28b23.output</output-file> <status>completed</status> <summary>Agent "Fix two broken copy gates" finished</summary> <note>A task-notification fires each time this agent stops with no live background children of its own. The user can send it another message and resume it, so the same task-id may notify more than once.</note> <result>Status: done. Both copy gates are fixed and the commit landed. The commit printed the stale lock error, which I left alone. Commit: `d07064d9` Tests, all run with the sandbox disabled: - `go test . -run 'Copy|Applied|Built|Instances' -count=1`: ok (6.4s) - `go test ./internal/docsite/ -count=1`: ok - `gofmt -l instances_cli.go`: no output Other failures: none. The working tree is clean after the commit. Changes: - **`instances_cli.go`**: the header line now reads `instancesListHeader+"\t"+"SEED"+"\t"+instancesColRuntime+"\t"+instancesColWorkers`. `"SEED"` is its own literal and the output is the same. - **`docs/site/cli.md`**: the "For a rootfs app, RUNTIME says" paragraph, unchanged, now sits directly after the `carlos instances retry --app hello shop-1.hello.example.com` fence, with one blank line before and after it.</result> <usage><subagent_tokens>38442</subagent_tokens><tool_uses>4</tool_uses><duration_ms>30950</duration_ms></usage> </task-notification>
Attributed to this commit: 12.1 g CO₂e (1,502,840 tokens). This is an attribution, not a measurement — usage counted from this commit's own marker forward to the next one on rootfs-apps-4-console, never backward from it. A turn that produced more than one commit books all of its tokens to the last one, and a concurrent session on this branch can add to the same figure. Some of it was priced using the shared default rate, because no model-specific factor row exists yet for at least one model involved. The Carbon view has the repo total, which is never derived from this attribution.
diff --git a/approved_rootfs_copy_test.go b/approved_rootfs_copy_test.go| index 228ce4787..08ef60b2e 100644 |
| --- a/approved_rootfs_copy_test.go |
| +++ b/approved_rootfs_copy_test.go |
| @@ -236,6 +236,16 @@ var rootfsCopyBuilt = []string{ |
| // Task 8b: carlos ship --check --env (ship_rootfs.go, docs/site/cli.md); |
| // C145 is Task 9's, in rails.md |
| "RFS-4-C141", "RFS-4-C142", "RFS-4-C143", "RFS-4-C144", "RFS-4-C146", |
| + // Plan 4 Task 9: the Rails guide, the Laravel and Django notes, and the Guides nav (docs/site/rails.md, laravel-and-django.md, nav.json) |
| + "RFS-4-C63", "RFS-4-C64", "RFS-4-C65", "RFS-4-C66", "RFS-4-C67", "RFS-4-C68", "RFS-4-C69", "RFS-4-C70", |
| + "RFS-4-C71", "RFS-4-C72", "RFS-4-C73", "RFS-4-C74", "RFS-4-C75", "RFS-4-C76", "RFS-4-C77", "RFS-4-C78", |
| + "RFS-4-C79", "RFS-4-C80", "RFS-4-C81", "RFS-4-C82", "RFS-4-C83", "RFS-4-C84", "RFS-4-C85", "RFS-4-C86", |
| + "RFS-4-C87", "RFS-4-C88", "RFS-4-C89", "RFS-4-C90", "RFS-4-C91", "RFS-4-C92", "RFS-4-C93", "RFS-4-C94", |
| + "RFS-4-C95", "RFS-4-C96", "RFS-4-C97", "RFS-4-C98", "RFS-4-C99", "RFS-4-C100", "RFS-4-C101", "RFS-4-C102", |
| + "RFS-4-C103", "RFS-4-C104", "RFS-4-C105", "RFS-4-C106", "RFS-4-C107", "RFS-4-C108", "RFS-4-C109", "RFS-4-C110", |
| + "RFS-4-C111", "RFS-4-C112", "RFS-4-C113", "RFS-4-C114", "RFS-4-C115", "RFS-4-C116", "RFS-4-C117", "RFS-4-C118", |
| + "RFS-4-C119", "RFS-4-C120", "RFS-4-C121", "RFS-4-C122", "RFS-4-C123", "RFS-4-C124", "RFS-4-C125", "RFS-4-C126", |
| + "RFS-4-C131", "RFS-4-C132", "RFS-4-C133", "RFS-4-C134", "RFS-4-C135", "RFS-4-C145", |
| } |
| func mustParseRootfsRecord(t *testing.T) []copyRow { |
diff --git a/docs/site/laravel-and-django.md b/docs/site/laravel-and-django.md| new file mode 100644 |
| index 000000000..73ef5826d |
| --- /dev/null |
| +++ b/docs/site/laravel-and-django.md |
| @@ -0,0 +1,65 @@ |
| +# Laravel and Django |
| + |
| +The Rails guide's shape works for any framework that runs on Linux. These notes cover what is different for Laravel and for Django. Read [the Rails guide](/docs/rails) first. |
| + |
| +## Laravel |
| + |
| +Serve Laravel with FrankenPHP, which is one HTTP server process. php-fpm speaks FastCGI, not HTTP, so it cannot be web on its own. Bind FrankenPHP to the socket in its Caddyfile: |
| + |
| +```text |
| +{ |
| + frankenphp |
| + admin off |
| +} |
| + |
| +http:// { |
| + bind unix/{$CARLOS_SOCKET} |
| + root * /app/public |
| + php_server |
| +} |
| +``` |
| + |
| +Point the sqlite connection at the instance's database in config/database.php, and set DB_CONNECTION=sqlite with carlos env set: |
| + |
| +```php |
| +'database' => env('CARLOS_DB'), |
| +``` |
| + |
| +The processes: web runs FrankenPHP, a worker runs the queue, release runs the migrations, and console runs tinker. With the database queue driver the jobs live in the same database. |
| + |
| +```json |
| +{ |
| + "web": {"command": ["frankenphp", "run", "--config", "/app/Caddyfile"], |
| + "env": {"XDG_DATA_HOME": "/tmp", "XDG_CONFIG_HOME": "/tmp"}}, |
| + "worker": {"command": ["php", "artisan", "queue:work"]}, |
| + "release": {"command": ["php", "artisan", "migrate", "--force"]}, |
| + "console": {"command": ["php", "artisan", "tinker"]} |
| +} |
| +``` |
| + |
| +Caddy keeps state in its data and config directories. Point them at /tmp with XDG_DATA_HOME and XDG_CONFIG_HOME in web's env, and turn Caddy's admin endpoint off. |
| + |
| +For uploads, Laravel's s3 disk takes the bucket, region and keys from CARLOS_STORE_BUCKET, CARLOS_STORE_REGION, CARLOS_STORE_ACCESS_KEY and CARLOS_STORE_SECRET_KEY in config/filesystems.php. |
| + |
| +Leave the opcache JIT off, as it is by default. opcache itself is fine. |
| + |
| +## Django |
| + |
| +Serve Django with Gunicorn, which binds a unix socket itself. Give it the path in gunicorn.conf.py, since commands are not run through a shell: |
| + |
| +```python |
| +import os |
| + |
| +bind = "unix:" + os.environ["CARLOS_SOCKET"] |
| +workers = 2 |
| +``` |
| + |
| +In settings.py, use the sqlite3 engine with NAME set to os.environ["CARLOS_DB"]. |
| + |
| +The processes: web runs gunicorn -c gunicorn.conf.py with your WSGI module, release runs python manage.py migrate --noinput, and console runs python manage.py shell. A worker is whatever your task queue runs. Pick one that keeps its jobs in the database, so it shares the instance's database. |
| + |
| +Collect static files when you build the image, and serve them with WhiteNoise. They only need to be read, so the read-only image is fine. |
| + |
| +For uploads, django-storages' S3 backend takes the bucket, region and keys from CARLOS_STORE_BUCKET, CARLOS_STORE_REGION, CARLOS_STORE_ACCESS_KEY and CARLOS_STORE_SECRET_KEY in settings.py. |
| + |
| +Python's own JIT is off unless you build Python with it. Leave it off. |
diff --git a/docs/site/nav.json b/docs/site/nav.json| index e1acfe9a0..dbcbfe3fd 100644 |
| --- a/docs/site/nav.json |
| +++ b/docs/site/nav.json |
| @@ -1,5 +1,20 @@ |
| { |
| "sections": [ |
| + { |
| + "title": "Guides", |
| + "pages": [ |
| + { |
| + "slug": "rails", |
| + "label": "Run a Rails app", |
| + "blurb": "Build a Rails 8 app into an image, ship it, and run it with a worker on your own boxes." |
| + }, |
| + { |
| + "slug": "laravel-and-django", |
| + "label": "Laravel and Django", |
| + "blurb": "What changes from the Rails guide for a Laravel or a Django app." |
| + } |
| + ] |
| + }, |
| { |
| "title": "Reference", |
| "pages": [ |
diff --git a/docs/site/rails.md b/docs/site/rails.md| new file mode 100644 |
| index 000000000..a1c8c7f14 |
| --- /dev/null |
| +++ b/docs/site/rails.md |
| @@ -0,0 +1,215 @@ |
| +# Run a Rails app on your own boxes |
| + |
| +This guide takes a Rails 8 app from a Dockerfile to a running instance on boxes your account owns. The app runs from a read-only image you build on your own machine. Its web process and its job worker share one SQLite database, which the platform replicates, and uploads go to the app's own bucket. |
| + |
| +You need Docker with buildx, the carlos CLI signed in to your console, and a fleet with at least one box. On a deployment that runs apps for other people, a rootfs app runs only on a fleet your account owns. [carlos fleets](/docs/cli#carlos-fleets) sets one up, and [Rootfs apps](/docs/rootfs) has the full contract. |
| + |
| +The finished app is in the platform repository under examples/rails-shop. Every file this guide shows is taken from it. |
| + |
| +## The app |
| + |
| +Start from rails new with SQLite. Rails 8 gives you Solid Queue, Solid Cache and Solid Cable, each with a database of its own. On CARLOS an instance has one database, so all three share it. |
| + |
| +### Puma |
| + |
| +The platform gives the web process a unix socket to serve on, in CARLOS_SOCKET. Bind Puma to it and to nothing else. A process that listens on a TCP port instead never answers its health check. |
| + |
| +```ruby |
| +# config/puma.rb |
| +threads_count = ENV.fetch("RAILS_MAX_THREADS", 3) |
| +threads threads_count, threads_count |
| + |
| +if ENV["CARLOS_SOCKET"] |
| + bind "unix://#{ENV["CARLOS_SOCKET"]}" |
| +else |
| + port ENV.fetch("PORT", 3000) |
| +end |
| + |
| +plugin :tmp_restart |
| +``` |
| + |
| +puma.rb reads the socket from the environment because commands are not run through a shell. Without CARLOS_SOCKET, on your laptop, it listens on port 3000 as usual. |
| + |
| +### One database |
| + |
| +CARLOS_DB is the path of the instance's database. It is the one file the platform replicates, and it is kept across restarts and deploys. |
| + |
| +```yaml |
| +# config/database.yml |
| +production: |
| + primary: |
| + <<: *default |
| + database: <%= ENV["CARLOS_DB"] %> |
| +``` |
| + |
| +Point Solid Queue, Solid Cache and Solid Cable at the same database. Remove config.solid_queue.connects_to from config/environments/production.rb, remove database: from the production section of config/cache.yml, and remove connects_to from the production section of config/cable.yml. Each then uses the primary connection. |
| + |
| +Their tables then need migrations of their own. Copy the table definitions from db/queue_schema.rb, db/cache_schema.rb and db/cable_schema.rb into three migrations, and delete the three schema files. |
| + |
| +```ruby |
| +# db/migrate/20261024000001_create_solid_queue_tables.rb |
| + create_table "solid_queue_blocked_executions", force: :cascade do |t| |
| + t.bigint "job_id", null: false |
| + t.string "queue_name", null: false |
| + t.integer "priority", default: 0, null: false |
| + t.string "concurrency_key", null: false |
| + t.datetime "expires_at", null: false |
| + t.datetime "created_at", null: false |
| + t.index [ "concurrency_key", "priority", "job_id" ], name: "index_solid_queue_blocked_executions_for_release" |
| + t.index [ "expires_at", "concurrency_key" ], name: "index_solid_queue_blocked_executions_for_maintenance" |
| + t.index [ "job_id" ], name: "index_solid_queue_blocked_executions_on_job_id", unique: true |
| + end |
| +``` |
| + |
| +SQLite runs in WAL mode, so the web process and the worker can both write to the file. |
| + |
| +### Uploads |
| + |
| +Keep uploads out of the image and out of the database. carlos store create gives the app a bucket and puts its coordinates in the app's environment. Point Active Storage at them: |
| + |
| +```yaml |
| +# config/storage.yml |
| +carlos: |
| + service: S3 |
| + bucket: <%= ENV["CARLOS_STORE_BUCKET"] %> |
| + region: <%= ENV["CARLOS_STORE_REGION"] %> |
| + access_key_id: <%= ENV["CARLOS_STORE_ACCESS_KEY"] %> |
| + secret_access_key: <%= ENV["CARLOS_STORE_SECRET_KEY"] %> |
| +<% if ENV["CARLOS_STORE_ENDPOINT"].present? %> |
| + endpoint: <%= ENV["CARLOS_STORE_ENDPOINT"] %> |
| + force_path_style: true |
| +<% end %> |
| +``` |
| + |
| +Then set config.active_storage.service = :carlos in production.rb, and add the aws-sdk-s3 gem. |
| + |
| +### YJIT |
| + |
| +Instances run with memory that cannot be written and executed at once, unless the release is shipped with carlos ship --jit, so YJIT cannot start without it. Rails 7.2 and later turn YJIT on by default. This app turns it off in production.rb with config.yjit = false. To keep YJIT, ship with --jit instead. |
| + |
| +## The image |
| + |
| +Build the image for arm64, which is what the boxes run. buildx does that on any machine, an Intel laptop included. When the image runs, its root filesystem is read-only, and only /tmp and the directory CARLOS_DB is in can be written. |
| + |
| +```dockerfile |
| +# Dockerfile |
| +FROM docker.io/library/ruby:3.4-slim AS base |
| +WORKDIR /rails |
| +ENV RAILS_ENV=production BUNDLE_DEPLOYMENT=1 BUNDLE_PATH=/usr/local/bundle \ |
| + BUNDLE_WITHOUT=development:test BOOTSNAP_CACHE_DIR=/rails/.bootsnap |
| +RUN apt-get update -qq && apt-get install --no-install-recommends -y libjemalloc2 libsqlite3-0 && \ |
| + ln -s /usr/lib/$(uname -m)-linux-gnu/libjemalloc.so.2 /usr/local/lib/libjemalloc.so && \ |
| + rm -rf /var/lib/apt/lists/* |
| +ENV LD_PRELOAD=/usr/local/lib/libjemalloc.so |
| + |
| +FROM base AS build |
| +RUN apt-get update -qq && apt-get install --no-install-recommends -y build-essential git libyaml-dev pkg-config && \ |
| + rm -rf /var/lib/apt/lists/* |
| +COPY Gemfile Gemfile.lock ./ |
| +RUN bundle install && rm -rf ~/.bundle "${BUNDLE_PATH}"/ruby/*/cache |
| +COPY . . |
| +RUN bundle exec bootsnap precompile --gemfile app/ lib/ && SECRET_KEY_BASE_DUMMY=1 bin/rails assets:precompile |
| + |
| +FROM base |
| +COPY --from=build /usr/local/bundle /usr/local/bundle |
| +COPY --from=build /rails /rails |
| +RUN rm -rf /rails/tmp && ln -s /tmp /rails/tmp && chmod -R a+rX /rails /usr/local/bundle |
| +ENV BOOTSNAP_READONLY=1 |
| +``` |
| + |
| +tmp is a link to /tmp, because Rails writes pid and cache files there. |
| + |
| +Bootsnap's cache is built into the image, and BOOTSNAP_READONLY keeps it from writing at run time. |
| + |
| +Every file is readable by any user, because an instance does not run as root. |
| + |
| +There is no ENTRYPOINT or CMD. The platform runs the commands processes.json names, and nothing else. |
| + |
| +## The processes |
| + |
| +processes.json, beside the Dockerfile, names the processes. carlos ship --processes reads it when it makes the release: |
| + |
| +```json |
| +{ |
| + "web": {"command": ["bundle", "exec", "puma", "-C", "config/puma.rb"], "working_dir": "/rails"}, |
| + "worker": {"command": ["/rails/bin/jobs"], "working_dir": "/rails"}, |
| + "release": {"command": ["/rails/bin/rails", "db:migrate"], "working_dir": "/rails"}, |
| + "console": {"command": ["/rails/bin/rails", "console"], "working_dir": "/rails"} |
| +} |
| +``` |
| + |
| +web serves requests. worker runs beside it, from the same image, with the same environment and the same database. |
| + |
| +release runs on every deploy, before the new web starts. Here it runs the migrations. |
| + |
| +console is the command an interactive session starts. The platform never starts it on its own. |
| + |
| +A rootfs app is always on. To let an instance sleep when nobody uses it, ship the app's first release with carlos ship --hibernate. An instance keeps the setting it was created with. |
| + |
| +## Check it on your machine |
| + |
| +carlos ship --check runs the image the way a box would: a read-only root, a private /tmp, a socket, a database, and the default memory limit. It needs Docker or Podman, and says what went wrong, such as a process that listened on a port, a write outside the instance's directory, or an image built for amd64. |
| + |
| +The check runs without your app's config and secrets, so give it placeholders for anything the app reads at boot. The sample needs a secret key and its store's coordinates: |
| + |
| +```sh |
| +docker buildx build --platform linux/arm64 --load -t shop:1 . |
| +docker save -o shop.tar shop:1 |
| +carlos ship --rootfs shop.tar --processes processes.json --check \ |
| + --env SECRET_KEY_BASE=placeholder \ |
| + --env CARLOS_STORE_BUCKET=placeholder --env CARLOS_STORE_REGION=eu-west-1 \ |
| + --env CARLOS_STORE_ACCESS_KEY=placeholder --env CARLOS_STORE_SECRET_KEY=placeholder |
| +``` |
| + |
| +## Ship it |
| + |
| +Create the app on your fleet, give it instances, a bucket and a secret key base. Then ship the image, promote it, and create the instance. |
| + |
| +```sh |
| +carlos apps create --app shop --place fleet/home |
| +carlos instances enable --app shop |
| +carlos store create --app shop |
| +carlos secrets set --app shop SECRET_KEY_BASE=$(openssl rand -hex 64) |
| +carlos ship --app shop --version v1 --rootfs shop.tar --processes processes.json |
| +carlos promote --app shop v1 |
| +carlos instances create --app shop --host shop.k3x.example.com |
| +``` |
| + |
| +Ship before you create the instance. An app's first release makes it a rootfs app, and an instance takes that, and whether it hibernates, from the release when it is created. |
| + |
| +The first start runs release, which migrates the empty database, then starts web, then the worker once web answers. carlos instances list shows the instance's runtime and its workers, and carlos logs shows all three processes. |
| + |
| +```sh |
| +carlos instances list --app shop |
| +carlos logs --app shop -f |
| +``` |
| + |
| +carlos run does not reach rootfs instances yet. |
| + |
| +## Deploys and migrations |
| + |
| +A deploy starts the new web beside the old one and moves traffic once the new one answers. release runs first, from the new image, while the old version is still serving. So a migration has to work with the code that is already running: add a column, deploy, use it, and remove the old one in a later deploy. |
| + |
| +When a migration cannot work that way, ship that release with --swap hold. The old version stops, release runs, and the new version starts. Requests wait for a few seconds while that happens. |
| + |
| +```sh |
| +carlos ship --app shop --version v7 --swap hold --rootfs shop.tar --processes processes.json |
| +``` |
| + |
| +If release fails or runs past its time limit, the old version keeps serving and the deploy stops for that instance. carlos instances list says so, and the next deploy tries again. |
| + |
| +Rolling back a release does not roll back the database. carlos rollback brings the previous image back, and it runs against the schema the newer release left. This is the other reason a migration should work with the code before it. |
| + |
| +```sh |
| +carlos rollback --app shop stable |
| +``` |
| + |
| +During a deploy the old worker stops once the new web is healthy, and then the new worker starts. Two workers never run at once. |
| + |
| +## When something crashes |
| + |
| +The platform starts the app when its box starts, starts web or a worker again when it crashes, and never waits for a visitor to do it. A worker that keeps crashing is started again with a growing delay, and never takes web down with it. |
| + |
| +## Limits |
| + |
| +[Rootfs apps](/docs/rootfs) lists the limits. The ones a Rails app meets first: one memory limit for all its processes together, 120 seconds for web to answer after it starts, and at most four workers. |
diff --git a/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md b/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md| index 8ec4c98bf..134e876b4 100644 |
| --- a/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md |
| +++ b/docs/superpowers/plans/2026-10-24-rootfs-apps-copy.md |
| @@ -375,11 +375,11 @@ From the plan: Placeholders: `<app>` an app; `<fleet>` a fleet name; `<region>` |
| | RFS-4-C128 | docs/site/cli.md (carlos apps) | carlos apps place refuses a rootfs app, whatever the target, because a rootfs app's instances do not move. Give a rootfs app its fleet with --place fleet/<name> when you create it, before its first ship. [Rootfs apps](/docs/rootfs) says why. | approved | |
| | RFS-4-C129 | docs/site/nav.json (label) | Rootfs apps | approved | |
| | RFS-4-C130 | docs/site/nav.json (blurb) | What a rootfs app's image is started with, what it can write, and where it may run. | approved | |
| -| RFS-4-C131 | `nav.json` label | Run a Rails app | approved | |
| -| RFS-4-C132 | `nav.json` blurb | Build a Rails 8 app into an image, ship it, and run it with a worker on your own boxes. | approved | |
| -| RFS-4-C133 | `nav.json` label | Laravel and Django | approved | |
| -| RFS-4-C134 | `nav.json` blurb | What changes from the Rails guide for a Laravel or a Django app. | approved | |
| -| RFS-4-C135 | `nav.json` section | Guides | approved | |
| +| RFS-4-C131 | docs/site/nav.json (label) | Run a Rails app | approved | |
| +| RFS-4-C132 | docs/site/nav.json (blurb) | Build a Rails 8 app into an image, ship it, and run it with a worker on your own boxes. | approved | |
| +| RFS-4-C133 | docs/site/nav.json (label) | Laravel and Django | approved | |
| +| RFS-4-C134 | docs/site/nav.json (blurb) | What changes from the Rails guide for a Laravel or a Django app. | approved | |
| +| RFS-4-C135 | docs/site/nav.json (section) | Guides | approved | |
| ### docs/site/rootfs.md, JIT rows |
diff --git a/internal/docsite/fences_test.go b/internal/docsite/fences_test.go| index 11f1e7c80..9424fc5dd 100644 |
| --- a/internal/docsite/fences_test.go |
| +++ b/internal/docsite/fences_test.go |
| @@ -19,6 +19,11 @@ var knownFenceLangs = map[string]bool{ |
| // that page exists to prevent (hex secret, raw-byte key) is the one they |
| // make in whatever language they are in. |
| "python": true, |
| + // dockerfile, ruby and php, for the Rails guide and the Laravel notes. |
| + // The guide quotes the sample app's Dockerfile and Ruby config word for |
| + // word (rails_sample_test.go holds it to that), and the Laravel notes |
| + // show the one line of config/database.php that differs. |
| + "dockerfile": true, "ruby": true, "php": true, |
| } |
| func TestFenceLanguagesAreDeclared(t *testing.T) { |
diff --git a/internal/docsite/rails_sample_test.go b/internal/docsite/rails_sample_test.go| new file mode 100644 |
| index 000000000..7c3776ca1 |
| --- /dev/null |
| +++ b/internal/docsite/rails_sample_test.go |
| @@ -0,0 +1,127 @@ |
| +package docsite |
| + |
| +import ( |
| + "encoding/json" |
| + "io/fs" |
| + "os" |
| + "path/filepath" |
| + "reflect" |
| + "strings" |
| + "testing" |
| +) |
| + |
| +const sampleRoot = "../../examples/rails-shop" |
| + |
| +// TestRailsGuideQuotesTheSample: every fence on rails.md whose first line is |
| +// "# <path>" has a body (the lines after it) that is a substring of |
| +// examples/rails-shop/<path>, and the page's one json fence decodes equal |
| +// (reflect.DeepEqual over map[string]any) to examples/rails-shop/processes.json. |
| +func TestRailsGuideQuotesTheSample(t *testing.T) { |
| + site, err := Load(root) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + var page *Page |
| + for _, p := range site.Pages() { |
| + if p.Slug == "rails" { |
| + page = p |
| + } |
| + } |
| + if page == nil { |
| + t.Fatal("no rails page") |
| + } |
| + quoted, jsons := 0, 0 |
| + for _, f := range page.Fences { |
| + if f.Lang == "json" { |
| + jsons++ |
| + var got, want map[string]any |
| + if err := json.Unmarshal([]byte(f.Body), &got); err != nil { |
| + t.Errorf("rails.md:%d: the json fence does not decode: %v", f.Line, err) |
| + continue |
| + } |
| + raw, err := os.ReadFile(filepath.Join(sampleRoot, "processes.json")) |
| + if err != nil { |
| + t.Errorf("rails.md:%d: %v", f.Line, err) |
| + continue |
| + } |
| + if err := json.Unmarshal(raw, &want); err != nil { |
| + t.Errorf("processes.json does not decode: %v", err) |
| + continue |
| + } |
| + if !reflect.DeepEqual(got, want) { |
| + t.Errorf("rails.md:%d: the json fence is not what examples/rails-shop/processes.json has", f.Line) |
| + } |
| + continue |
| + } |
| + first, body, _ := strings.Cut(f.Body, "\n") |
| + path, ok := strings.CutPrefix(first, "# ") |
| + if !ok || strings.Contains(path, " ") { |
| + continue |
| + } |
| + raw, err := os.ReadFile(filepath.Join(sampleRoot, path)) |
| + if err != nil { |
| + t.Errorf("rails.md:%d quotes %s: %v", f.Line, path, err) |
| + continue |
| + } |
| + if !strings.Contains(string(raw), body) { |
| + t.Errorf("rails.md:%d: the quoted %s is not what examples/rails-shop has", f.Line, path) |
| + } |
| + quoted++ |
| + } |
| + if quoted < 4 { |
| + t.Fatalf("rails.md quotes %d sample files, want puma.rb, database.yml, storage.yml and the Dockerfile at least", quoted) |
| + } |
| + if jsons != 1 { |
| + t.Fatalf("rails.md has %d json fences, want the one processes.json", jsons) |
| + } |
| +} |
| + |
| +// TestRailsSampleHasItsFiles: every path the sample must have exists, and the |
| +// ones it must not have do not. |
| +func TestRailsSampleHasItsFiles(t *testing.T) { |
| + must := []string{ |
| + "README.md", "Dockerfile", "processes.json", "Gemfile", "Gemfile.lock", |
| + "config/puma.rb", "config/database.yml", "config/storage.yml", |
| + "config/cache.yml", "config/cable.yml", "config/recurring.yml", |
| + "config/environments/production.rb", "config/routes.rb", |
| + "app/models/ping.rb", "app/models/heartbeat.rb", "app/models/upload.rb", |
| + "app/jobs/ping_job.rb", "app/jobs/heartbeat_job.rb", "app/jobs/crash_worker_job.rb", |
| + "app/controllers/pages_controller.rb", "app/controllers/pings_controller.rb", |
| + "app/controllers/uploads_controller.rb", "app/controllers/crashes_controller.rb", |
| + "app/views/pages/show.html.erb", "app/views/layouts/application.html.erb", |
| + "db/schema.rb", ".dockerignore", "v2.patch", |
| + } |
| + for _, p := range must { |
| + if _, err := os.Stat(filepath.Join(sampleRoot, p)); err != nil { |
| + t.Errorf("the sample lacks %s: %v", p, err) |
| + } |
| + } |
| + for n := 1; n <= 7; n++ { |
| + prefix := "2026102400000" + string(rune('0'+n)) + "_*.rb" |
| + m, err := filepath.Glob(filepath.Join(sampleRoot, "db/migrate", prefix)) |
| + if err != nil || len(m) != 1 { |
| + t.Errorf("db/migrate/%s: want exactly one file, got %v (%v)", prefix, m, err) |
| + } |
| + } |
| + mustNot := []string{ |
| + "db/queue_schema.rb", "db/cache_schema.rb", "db/cable_schema.rb", |
| + "bin/docker-entrypoint", |
| + } |
| + for _, p := range mustNot { |
| + if _, err := os.Stat(filepath.Join(sampleRoot, p)); err == nil { |
| + t.Errorf("the sample has %s, which it must not", p) |
| + } |
| + } |
| + err := filepath.WalkDir(sampleRoot, func(p string, d fs.DirEntry, err error) error { |
| + if err != nil { |
| + return err |
| + } |
| + if !d.IsDir() && strings.HasSuffix(p, ".go") { |
| + t.Errorf("the sample holds a Go file: %s", p) |
| + } |
| + return nil |
| + }) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| +} |