or run

tessl search
Log in

Version

Files

tile.json

task.mdevals/scenario-3/

Path Resolution Utility

Overview

Create a utility that resolves file paths to their canonical absolute paths, handling symbolic links and relative paths.

Requirements

You need to implement a Node.js module that provides a function to resolve paths to their canonical form. The function should:

  1. Accept a file path (relative or absolute) as input
  2. Return the canonical absolute path, resolving all symbolic links in the path
  3. Throw an error if the path doesn't exist
  4. Handle edge cases like circular symbolic links appropriately

Implementation Details

Create a file pathResolver.js that exports a function named resolveCanonicalPath:

  • The function should accept one parameter: inputPath (string)
  • The function should return the canonical absolute path (string)
  • If the path contains symbolic links, they should be fully resolved
  • The function should work with both relative and absolute paths

Dependencies { .dependencies }

metro-memory-fs { .dependency }

Provides an in-memory filesystem implementation for testing purposes.

Test Cases

Test Case 1: Resolve Absolute Path { @test }

// pathResolver.test.js
const MemoryFs = require('metro-memory-fs');
const { resolveCanonicalPath } = require('./pathResolver');

const fs = new MemoryFs();
fs.writeFileSync('/home/user/document.txt', 'content');

const result = resolveCanonicalPath(fs, '/home/user/document.txt');
// Expected: '/home/user/document.txt'

Test Case 2: Resolve Symbolic Link { @test }

// pathResolver.test.js
const MemoryFs = require('metro-memory-fs');
const { resolveCanonicalPath } = require('./pathResolver');

const fs = new MemoryFs();
fs.writeFileSync('/home/target.txt', 'data');
fs.symlinkSync('/home/target.txt', '/home/link.txt');

const result = resolveCanonicalPath(fs, '/home/link.txt');
// Expected: '/home/target.txt'

Test Case 3: Resolve Nested Symbolic Links { @test }

// pathResolver.test.js
const MemoryFs = require('metro-memory-fs');
const { resolveCanonicalPath } = require('./pathResolver');

const fs = new MemoryFs();
fs.mkdirSync('/real/path', { recursive: true });
fs.writeFileSync('/real/path/file.txt', 'content');
fs.symlinkSync('/real/path', '/link1');
fs.symlinkSync('/link1', '/link2');

const result = resolveCanonicalPath(fs, '/link2/file.txt');
// Expected: '/real/path/file.txt'

Constraints

  • The solution should handle symbolic links correctly
  • Error handling should be appropriate for non-existent paths
  • The implementation should be concise and straightforward