closed-form analytical expression for DFT coefficients
The DFT of a vector of size L is L samples of the Fourier transform of the signal:

$$
X[k] = X(e^{j\theta})|_{\theta = 2\pi k/L}
$$

So now we need to calculate the FT of $x[n]$:

$$
X(e^{j\theta}) = \sum_{n=0}^{n=M-1} e^{-j n \theta}
$$

This is a geometric series which results in:

$$
X(e^{j\theta}) = \frac{1 - e^{-jM\theta}}{1 - e^{-j\theta}}
$$

Evaluate on the DFT samples to give:

$$
X[k] = \frac{1 - e^{-j M 2 \pi k/L}}{1 - e^{-j 2 \pi k / L}}
$$

You can extract half phase from top and bottom to give:

$$
X[k] = e^{-j(M-1)\pi k/L} \frac{\sin(M \pi k/L)}{\sin(\pi k/L)}
$$