CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-shebang-regex

Regular expression for matching Unix shebang lines at the beginning of files

88

1.54x
Overview
Eval results
Files

task.mdevals/scenario-5/

Script File Validator

Build a script file validation tool that checks whether uploaded text files are valid executable scripts. The validator must detect whether files have proper shebang lines and classify the validation results.

Requirements

File Validation

Your validator must check if a file is a valid executable script by verifying that it starts with a shebang line. A valid shebang line:

  • Must be at the very beginning of the file (first character must be #)
  • Cannot have any leading whitespace or blank lines
  • Must follow the format #! followed by an interpreter path

Classification

The validator should classify files into three categories:

  1. Valid Scripts: Files that begin with a proper shebang line
  2. Invalid Scripts: Files that contain a shebang line but not at the very start (e.g., preceded by whitespace or other content)
  3. Non-Scripts: Files that do not contain any shebang line at all

Output Format

Your validator should process an array of file content strings and return a classification result object with three arrays:

{
  valid: ['file content 1', ...],      // Files with proper shebangs at the start
  invalid: ['file content 2', ...],    // Files with misplaced shebangs
  nonScripts: ['file content 3', ...]  // Files without any shebang
}

Implementation

@generates

API

/**
 * Validates and classifies file contents based on shebang line presence and position.
 *
 * @param {string[]} fileContents - Array of file content strings to validate
 * @returns {Object} Classification object with valid, invalid, and nonScripts arrays
 */
function validateScripts(fileContents) {
  // IMPLEMENTATION HERE
}

module.exports = { validateScripts };

Test Cases

  • Given a file starting with #!/bin/bash\necho "test", it is classified as valid @test
  • Given a file with leading newline \n#!/usr/bin/env node\nconsole.log(), it is classified as invalid @test
  • Given a file with leading whitespace #!/bin/sh\nls, it is classified as invalid @test
  • Given a file with no shebang console.log("hello world"), it is classified as non-script @test
  • Given multiple files, each is correctly classified into the appropriate category @test

Dependencies { .dependencies }

shebang-regex { .dependency }

Provides regular expression for matching shebang lines in Unix scripts.

Install with Tessl CLI

npx tessl i tessl/npm-shebang-regex

tile.json