Combining Path Components

A path library can join a base folder and relative components under the rules of the target operating system.

Source
Anbloq
Language
English
Created
Updated
Chapters
4
Reading time
1 min
In this article4 chapters
  1. Trace a simple join
  2. Validate the purpose of a component
  3. Verify the resulting root as well as the final name
  4. Keep format-dependent cases in tests

Trace a simple join

Combining a project folder, the component data, and sales.csv constructs a location under that folder. A final component that is already absolute can change how common libraries resolve the result. See absolute and relative paths.

Validate the purpose of a component

A component chosen by a caller may be intended as a single filename. Check that it fits that contract before joining it. Domain validation distinguishes what a path API can represent from what a particular application accepts.[1]

Verify the resulting root as well as the final name

Joining base /example/reports with relative component today.csv gives /example/reports/today.csv. On POSIX-style paths, joining an absolute second component such as /tmp/today.csv discards the earlier base. An interface accepting only a relative filename should validate that contract. See absolute and relative paths.

Base /example/reports + today.csv → /example/reports/today.csv
Base /example/reports + /tmp/today.csv → /tmp/today.csv under POSIX joining
A join performs path semantics, not a textual separator paste

Keep format-dependent cases in tests

Test empty components, trailing separators and platform-specific rooted names. If path scope belongs to a higher-level contract, implement that validation separately and account for how the filesystem actually resolves names.

References

  1. Python Software Foundation, io: Core Tools for Working with Streams ↩